Skip to content

Instantly share code, notes, and snippets.

@orlv
Created June 21, 2017 19:26
Show Gist options
  • Select an option

  • Save orlv/cb3ac0f536a63b3bca41bf8e0f384cd6 to your computer and use it in GitHub Desktop.

Select an option

Save orlv/cb3ac0f536a63b3bca41bf8e0f384cd6 to your computer and use it in GitHub Desktop.

Revisions

  1. orlv created this gist Jun 21, 2017.
    51 changes: 51 additions & 0 deletions click.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    // File:
    // click.m
    //
    // Compile with:
    // gcc -o click click.m -framework ApplicationServices -framework Foundation
    //
    // Usage:
    // ./click -x pixels -y pixels
    // At the given coordinates it will click and release.


    #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"];


    // 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 = x;
    pt.y = y;


    // This is where the magic happens. See CGRemoteOperation.h for details.
    //
    // CGPostMouseEvent( CGPoint mouseCursorPosition,
    // boolean_t updateMouseCursorPosition,
    // CGButtonCount buttonCount,
    // boolean_t mouseButtonDown, ... )
    //
    // So, we feed coordinates to CGPostMouseEvent, put the mouse there,
    // then click and release.
    //
    CGPostMouseEvent( pt, 1, 1, 1 );
    CGPostMouseEvent( pt, 1, 1, 0 );


    [pool release];
    return 0;
    }