Skip to content

Instantly share code, notes, and snippets.

@SilverEzhik
Created August 31, 2018 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilverEzhik/6ea6f01b1a1712f03785cb288a662671 to your computer and use it in GitHub Desktop.
Save SilverEzhik/6ea6f01b1a1712f03785cb288a662671 to your computer and use it in GitHub Desktop.
This small program performs a middle click on macOS - stick this in an Automator service and you can get a shortcut to perform a middle click!
#include <ApplicationServices/ApplicationServices.h>
int main() {
// get pointer location
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
CFRelease(ourEvent);
// button down
CGEventRef click1_down = CGEventCreateMouseEvent(
NULL,
kCGEventOtherMouseDown,
point,
kCGMouseButtonCenter
);
CGEventPost(kCGHIDEventTap, click1_down);
// button up
CGEventRef click1_up = CGEventCreateMouseEvent(
NULL,
kCGEventOtherMouseUp,
point,
kCGMouseButtonCenter
);
CGEventPost(kCGHIDEventTap, click1_up);
// release the events
CFRelease(click1_down);
CFRelease(click1_up);
sleep(1); // fixes dragging getting stuck - issue with CGEventPost not actually finishing the job in time?
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment