3
votes

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.

2
This probably belongs on superuser.com.Rob Keniger
If he wants to know how to implement it using his own code then it's a legit programming question - but existing third party solutions are probably simpler.nekomatic

2 Answers

2
votes

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.

2
votes

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);