Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // Complile:
  2. // clang -o gittumouse gittumouse.c -framework ApplicationServices
  3.  
  4. #include <ApplicationServices/ApplicationServices.h>
  5.  
  6. static CGRect screenBounds;
  7.  
  8. // callback for mouse click.
  9. CGEventRef
  10. myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
  11. CGEventRef event, void *refcon)
  12. {
  13. int btn;
  14. // Do some sanity check.
  15. if (type != kCGEventOtherMouseDown)
  16. return event;
  17.  
  18. CGPoint location = CGEventGetLocation(event);
  19.  
  20. btn = CGEventGetIntegerValueField(event, kCGMouseEventButtonNumber);
  21.  
  22. printf("(%f, %f) %i\n", location.x, location.y, btn);
  23.  
  24. return event;
  25. }
  26.  
  27. int
  28. main(void)
  29. {
  30. CFMachPortRef eventTap;
  31. CGEventMask eventMask;
  32. CFRunLoopSourceRef runLoopSource;
  33.  
  34. // The screen size of the primary display.
  35. screenBounds = CGDisplayBounds(CGMainDisplayID());
  36. printf("The main screen is %dx%d\n", (int)screenBounds.size.width,
  37. (int)screenBounds.size.height);
  38.  
  39. // Create an event tap. We are interested in kCGEventOtherMouseDown.
  40. eventMask = (1 << kCGEventOtherMouseDown);
  41. eventTap = CGEventTapCreate(
  42. kCGSessionEventTap, kCGHeadInsertEventTap,
  43. 0, eventMask, myCGEventCallback, NULL);
  44. if (!eventTap) {
  45. fprintf(stderr, "failed to create event tap\n");
  46. exit(1);
  47. }
  48.  
  49. // Create a run loop source.
  50. runLoopSource = CFMachPortCreateRunLoopSource(
  51. kCFAllocatorDefault, eventTap, 0);
  52.  
  53. // Add to the current run loop.
  54. CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
  55. kCFRunLoopCommonModes);
  56.  
  57. // Enable the event tap.
  58. CGEventTapEnable(eventTap, true);
  59.  
  60. // Set it all running.
  61. CFRunLoopRun();
  62.  
  63. // In a real program, one would have arranged for cleaning up.
  64.  
  65. exit(0);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement