Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement