Reputation: 2456
I would like to be able to assign a key on my keyboard to be equivalent to a left mouse click.
Ideally it needs to act such that holding the key down is also equivalent to holding the left mouse button down.
I'd like this capability as a user, additionally a programmatic solution (cocoa/applescript etc) would be great too.
Upvotes: 3
Views: 1862
Reputation: 2456
This can be done by writing some code:
Write a global handler to receive the type of event you want to watch
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask
handler:^(NSEvent *event){
NSLog(@"%i", [event keyCode]);
//todo invoke mouse clicking code;
}];
Then write the mouse click code:
// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
// perform a click
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
Upvotes: 2
Reputation: 63952
Not exactly what you want, but in the System preferences -> Universal access you can turn on mouse keys - and with them you can move (and click) mouse by keyboard. docs here:
Or, With the "ControllerMate.app" is possible to do this, but it is commercial app.
Upvotes: 2