Skip to content

Instantly share code, notes, and snippets.

@sgallese
Created March 17, 2010 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgallese/335106 to your computer and use it in GitHub Desktop.
Save sgallese/335106 to your computer and use it in GitHub Desktop.
// File:
// customclick.m
//
// clickdrag will drag the "mouse" from the current cursor position to
// the given coordinates.
//
// derived from:
// http://www.macosxhints.com/article.php?story=2008051406323031
// &
// http://davehope.co.uk/Blog/osx-click-the-mouse-via-code/
//
// Requires:
// xtools
//
// Compile with:
// gcc -o customclick customclick.m -framework ApplicationServices -framework Foundation
//
// Usage:
// ./clickdrag
//
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
// grabs command line arguments -x and -y
//
// int x = [args integerForKey:@"x"];
//int y = [args integerForKey:@"y"];
// This is where the magic happens. See CGRemoteOperation.h for details.
// Get the current mouse location
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint ourLoc = CGEventGetLocation(ourEvent);
// The data structure CGPoint represents a point in a two-dimensional
// coordinate system. Here, X and Y distance from upper left, in pixels.
//
CGPoint pt;
pt.x = ourLoc.x + 320;
pt.y = ourLoc.y + 240;
// CGPostMouseEvent( CGPoint mouseCursorPosition,
// boolean_t updateMouseCursorPosition,
// CGButtonCount buttonCount,
// boolean_t mouseButtonDown, ... )
CGEventRef mouseDownEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseDown,ourLoc,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseDownEv);
CGEventRef mouseUpEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseUp,pt,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseUpEv );
CGEventRef mouseLastEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseUp,ourLoc,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseLastEv );
// Start drag from current cursor location
//CGPostMouseEvent( ourLoc, TRUE, 1, FALSE );
//CGPostMouseEvent( ourLoc, TRUE, 1, TRUE );
// End drag by moving to new location
//CGPostMouseEvent( pt, TRUE, 1, TRUE );
//CGPostMouseEvent( pt, FALSE, 1, FALSE );
//Go back to start position
//CGPostMouseEvent( ourLoc, TRUE, 1, FALSE );
// Possible sleep routines
//usleep(100000);
//sleep(2);
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment