Created
August 31, 2018 02:44
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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