Sometimes, you could have the need to generate mouse movements or clicks from your application.

With Mac OS X this goal can be achieved fast by using the ApplicationServices framework.

With this framework you can generate a complete set of Mouse events:

  • Left Button Down
  • Right Button Down
  • Left Button Up
  • Right Button Up
  • Mouse Move

The procedure is the following:

  1. Create a new Mouse Event
  2. Post the Event
  3. Release the Event.

To create a new event you have to use the CGEventCreateMouseMovement API

To post an event you have to use CGEventPost API

To release an event you have to use CFRelease API


So, for example, imagine you want to move your mouse to the position (200, 200) and then you want to do a double click, you can use the following program:



//  SimpleEvents.c

//  MouseEvent


//  Created by Luigi D'Andrea on 30/12/12.


int main(int argc, char **argv)


    int posx = 200;

    int posy = 200;

    // Move to posxXposy

    CGEventRef move1 = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, CGPointMake(posx, posy), kCGMouseButtonLeft );

    CGEventPost(kCGHIDEventTap, move1);



    // Left button down at posxXposy

    CGEventRef click1_down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(posx, posy), kCGMouseButtonLeft);

    // Left button up at posxXposy

    CGEventRef click1_up = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(posx, posy), kCGMouseButtonLeft);


    CGEventPost(kCGHIDEventTap, click1_down);

    CGEventPost(kCGHIDEventTap, click1_up);

    CGEventPost(kCGHIDEventTap, click1_down);

    CGEventPost(kCGHIDEventTap, click1_up);

    // Release the events




To compile it you can use the gcc, issue the following command:

$ gcc -o SimpleEvents SimpleEvents.c -framework ApplicationServices

and run it:

$ ./SimpleEvents