• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Simulating mouse events
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Simulating mouse events


  • Subject: Simulating mouse events
  • From: Sami Nopanen <email@hidden>
  • Date: Mon, 9 Nov 2009 01:14:43 -0500

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@hidden)

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:

This email sent to email@hidden

  • Prev by Date: Re: principle classes from two bundles get confused
  • Next by Date: Re: NSTreeController and object retain counts
  • Previous by thread: Re: iPhone: NSOperation and UITableVIews
  • Next by thread: QTMovie Load Percent.
  • Index(es):
    • Date
    • Thread