I'm trying to send simulated mouse events to a foreign application. I found
example code to do this from the mailing list archive, but have had some
unexpected behaviour when playing around with it.

The example creates a window that forwards mouse events to another (foreign)
window. But for some reason, it seems the the foreign application
receives the events with a large offset in the y-coordinate, while the
x-coordinate is correct. Also, the y-coordinate offset seems to be different
every time I run the event forwarding application.

Does anyone see what's going wrong here, and how I can fix it. (Running
Leopard 10.5.8 in case the OSX version might have something to do with
this).

Thanks,
Sami
--


#import <Cocoa/Cocoa.h>

@interface EventForwardingView : NSView
{
}

- (void)forwardEvent: (NSEvent *)event;

@end

--

#import "EventForwardingView.h"

#import <Foundation/Foundation.h>

#define PID     363   // The process ID of the process that events should be
forwarded to (get from Quartz Debug)
#define WID     76   // The window ID of the window of the process that
events are being forwarded to (get from Quartz Debug)

@implementation EventForwardingView

- (void)mouseDown: (NSEvent *)event {[self forwardEvent: event];}
- (void)rightMouseDown: (NSEvent *)event {[self forwardEvent: event];}
- (void)otherMouseDown: (NSEvent *)event {[self forwardEvent: event];}
- (void)mouseUp: (NSEvent *)event {[self forwardEvent: event];}
- (void)rightMouseUp: (NSEvent *)event {[self forwardEvent: event];}
- (void)otherMouseUp: (NSEvent *)event {[self forwardEvent: event];}
- (void)mouseMoved: (NSEvent *)event {[self forwardEvent: event];}
- (void)mouseDragged: (NSEvent *)event {[self forwardEvent: event];}
- (void)rightMouseDragged: (NSEvent *)event {[self forwardEvent: event];}
- (void)otherMouseDragged: (NSEvent *)event {[self forwardEvent: event];}

- (void)forwardEvent: (NSEvent *)event
{
    ProcessSerialNumber psn;
    CGEventRef CGEvent;
    NSEvent *customEvent;

    NSPoint point = [event locationInWindow];

    NSLog(@"Received event:");
    NSLog([event description]);

    customEvent = [NSEvent mouseEventWithType: [event type]
                                     location: point
                                modifierFlags: [event modifierFlags]
                                    timestamp: [event timestamp]
                                 windowNumber: WID
                                      context: nil
                                  eventNumber: [event eventNumber]
                                   clickCount: [event clickCount]
                                     pressure: [event pressure]];
    NSLog(@"Sending event:");
    NSLog([customEvent description]);

    CGEvent = [customEvent CGEvent];
    NSAssert(GetProcessForPID(PID, &psn) == noErr, @"GetProcessForPID
failed!");
    CGEventPostToPSN(&psn, CGEvent);
}

@end
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to