Skip to content

Instantly share code, notes, and snippets.

@infusion
Last active June 21, 2016 11:22
Show Gist options
  • Save infusion/a2874accee64d17f92a3 to your computer and use it in GitHub Desktop.
Save infusion/a2874accee64d17f92a3 to your computer and use it in GitHub Desktop.
Simulate clicks on OSX with C
// gcc -o click click.c -Wall -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
// Coord to click on
#define X 422
#define Y 192
int main() {
// Create MouseDown event
CGEventRef click1_down = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDown,
CGPointMake(X, Y),
kCGMouseButtonLeft
);
// Create MouseUp event
CGEventRef click1_up = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseUp,
CGPointMake(X, Y),
kCGMouseButtonLeft
);
// Click 100x
for (int i = 0; i < 100; i++) {
// Mouse down
CGEventPost(kCGHIDEventTap, click1_down);
sleep(1);
// Mouse up
CGEventPost(kCGHIDEventTap, click1_up);
sleep(1);
}
// Release the events
CFRelease(click1_up);
CFRelease(click1_down);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment