Remap `fn` to left mouse button on OSX

强颜欢笑 提交于 2019-12-08 17:39:48

问题


I get bad tendinitis from clicking the mouse all day.

In the past I used Karabiner to remap the fn key to simulate a left mouse button. However it doesn't work with Sierra.

I tried to accomplish this in Cocoa, and it correctly performs mouse-down/up when I press and release fn.

However it doesn't handle double-click / triple-click.

Also when dragging, (e.g. dragging a window, or selecting some text) nothing happens visually until I key-up, whereupon it completes.

How can I adapt my code to implement this?


First I create an event tap:

- (BOOL)tapEvents
{
    _modifiers = [NSEvent modifierFlags];

    if ( ! _eventTap ) {
        NSLog( @"Initializing an event tap." );

        // kCGHeadInsertEventTap -- new event tap should be inserted before
        //   any pre-existing event taps at the same location,
        _eventTap = CGEventTapCreate( kCGHIDEventTap, // kCGSessionEventTap,
                                      kCGHeadInsertEventTap,
                                      kCGEventTapOptionDefault,
                                           CGEventMaskBit( kCGEventKeyDown )
                                         | CGEventMaskBit( kCGEventFlagsChanged )
                                         | CGEventMaskBit( NSSystemDefined )
                                         ,
                                      (CGEventTapCallBack)_tapCallback,
                                      (__bridge void *)(self));
        if ( ! _eventTap ) {
            NSLog(@"unable to create event tap. must run as root or "
                    "add privlidges for assistive devices to this app.");
            return NO;
        }
    }
    CGEventTapEnable( _eventTap, YES );

    return [self isTapActive];
}

Now I implement the callback:

CGEventRef _tapCallback(
                        CGEventTapProxy proxy,
                        CGEventType     type,
                        CGEventRef      event,
                        Intercept*     listener
                        )
{
    //Do not make the NSEvent here.
    //NSEvent will throw an exception if we try to make an event from the tap timout type
    @autoreleasepool {
        if( type == kCGEventTapDisabledByTimeout ) {
            NSLog(@"event tap has timed out, re-enabling tap");
            [listener tapEvents];
            return nil;
        }
        if( type != kCGEventTapDisabledByUserInput ) {
            return [listener processEvent:event];
        }
    }
    return event;
}

Finally I implement a processEvent that will pass through any event apart from fn key up/down, which will get converted to left mouse up/down:

- (CGEventRef)processEvent:(CGEventRef)cgEvent
{
    //NSLog( @"- - - - - - -" );

    NSEvent* event = [NSEvent eventWithCGEvent:cgEvent];

    //NSLog(@"%d,%d", event.data1, event.data2);
    //NSEventType type = [event type];

    NSUInteger m = event.modifierFlags &
        ( NSCommandKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSControlKeyMask | NSAlphaShiftKeyMask | NSFunctionKeyMask );

    NSUInteger flags_changed = _modifiers ^ m;
    _modifiers = m;

    switch( event.type ) {
        case NSFlagsChanged:
        {
            assert(flags_changed);

            //NSLog(@"NSFlagsChanged: %d, event.modifierFlags: %lx", event.keyCode, event.modifierFlags);
            if( flags_changed & NSFunctionKeyMask ) {
                bool isDown = _modifiers & NSFunctionKeyMask;
                CGEventType evType = isDown ? kCGEventLeftMouseDown : kCGEventLeftMouseUp;
                CGPoint pt = [NSEvent mouseLocation];
                CGPoint mousePoint = CGPointMake(pt.x, [NSScreen mainScreen].frame.size.height - pt.y);

                CGEventRef theEvent = CGEventCreateMouseEvent(NULL, evType, mousePoint, kCGMouseButtonLeft);
                CGEventSetType(theEvent, evType);
                CGEventPost(kCGHIDEventTap, theEvent);
                CFRelease(theEvent);

                //return theEvent;
            }

            break;
        }
    }

    _lastEvent = [event CGEvent];
    CFRetain(_lastEvent); // must retain the event. will be released by the system
    return _lastEvent;
}

EDIT: Performing a double click using CGEventCreateMouseEvent()

EDIT: OSX assign left mouse click to a keyboard key

来源:https://stackoverflow.com/questions/44261049/remap-fn-to-left-mouse-button-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!