Skip to content

Instantly share code, notes, and snippets.

@sarim
Created December 18, 2014 16:15
Show Gist options
  • Save sarim/d9405309c3a02dbcaed7 to your computer and use it in GitHub Desktop.
Save sarim/d9405309c3a02dbcaed7 to your computer and use it in GitHub Desktop.
// Complile:
// clang -o gittumouse gittumouse.c -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
static CGRect screenBounds;
// callback for mouse click.
CGEventRef
myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void *refcon)
{
int btn;
// Do some sanity check.
if (type != kCGEventOtherMouseDown)
return event;
CGPoint location = CGEventGetLocation(event);
btn = CGEventGetIntegerValueField(event, kCGMouseEventButtonNumber);
printf("(%f, %f) %i\n", location.x, location.y, btn);
return event;
}
int
main(void)
{
CFMachPortRef eventTap;
CGEventMask eventMask;
CFRunLoopSourceRef runLoopSource;
// The screen size of the primary display.
screenBounds = CGDisplayBounds(CGMainDisplayID());
printf("The main screen is %dx%d\n", (int)screenBounds.size.width,
(int)screenBounds.size.height);
// Create an event tap. We are interested in kCGEventOtherMouseDown.
eventMask = (1 << kCGEventOtherMouseDown);
eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap,
0, eventMask, myCGEventCallback, NULL);
if (!eventTap) {
fprintf(stderr, "failed to create event tap\n");
exit(1);
}
// Create a run loop source.
runLoopSource = CFMachPortCreateRunLoopSource(
kCFAllocatorDefault, eventTap, 0);
// Add to the current run loop.
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
// Enable the event tap.
CGEventTapEnable(eventTap, true);
// Set it all running.
CFRunLoopRun();
// In a real program, one would have arranged for cleaning up.
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment