Skip to content

Instantly share code, notes, and snippets.

@vivkin
Created January 27, 2014 21:56
Show Gist options
  • Save vivkin/8658177 to your computer and use it in GitHub Desktop.
Save vivkin/8658177 to your computer and use it in GitHub Desktop.
auto OS X mouse click with delay
// clang mouse-click.c -o mouse-click -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "usage: %s [seconds to wait] [number of clicks]\n", argv[0]);
exit(EXIT_FAILURE);
}
int wait = strtol(argv[1], NULL, 10);
int count = strtol(argv[2], NULL, 10);
CGEventRef event = CGEventCreate(NULL);
CGPoint mouse_pos = CGEventGetLocation(event);
CFRelease(event);
fprintf(stderr, "Mouse x: %.1f, y: %1f\n", mouse_pos.x, mouse_pos.y);
CGEventRef mouse_down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, mouse_pos, kCGMouseButtonLeft);
CGEventRef mouse_up = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, mouse_pos, kCGMouseButtonLeft);
do
{
fprintf(stderr, "...%d\n", wait);
sleep(1);
}
while (wait--);
do
{
if (count % 500 == 0) fprintf(stderr, "...%d clicks left\n", count);
CGEventPost(kCGHIDEventTap, mouse_down);
CGEventPost(kCGHIDEventTap, mouse_up);
usleep(1000);
}
while (count--);
CFRelease(mouse_down);
CFRelease(mouse_up);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment