3

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.

zadam
  • 2,416
  • 2
  • 23
  • 32
  • 1
    This probably belongs on [superuser.com](http://superuser.com). – Rob Keniger Jun 11 '11 at 02:47
  • 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 Jun 13 '11 at 08:18

2 Answers2

2

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.

clt60
  • 62,119
  • 17
  • 107
  • 194
2

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);
zadam
  • 2,416
  • 2
  • 23
  • 32
  • I've been trying to solve the same problem here: https://stackoverflow.com/questions/44261049/remap-fn-to-left-mouse-button-on-osx – P i Jun 01 '17 at 20:35