简体   繁体   English

CGEventTapCreate 检测多个键盘输入

[英]CGEventTapCreate detecting multiple keyboard inputs

I'm working on a macOS menubar app that locks any mouse events.我正在开发一个锁定所有鼠标事件的 macOS 菜单栏应用程序。 What I'm trying to achieve is that after sending mouse events to the CGEventRef based callback, I cannot click anywhere (naturally) but the problem is I cannot quit the loop because of that.我想要实现的是,在将鼠标事件发送到基于CGEventRef的回调后,我无法(自然地)点击任何地方,但问题是我无法因此退出循环。

Every time I need to close it, I'm switching to the Xcode app to stop the running app.每次我需要关闭它时,我都会切换到 Xcode 应用程序以停止正在运行的应用程序。

This is the main function and the events that I'm sending to the callback function;这是主要的 function 和我发送给回调 function 的事件;

void lockTrackpad(void) {
    // For keyboard inputs, I add CGEventMaskBit(kCGEventKeyUp)| CGEventMaskBit(kCGEventKeyDown)| CGEventMaskBit(NX_SYSDEFINED)
    CGEventMask mask = (
                        CGEventMaskBit(kCGEventMouseMoved)
                        | CGEventMaskBit(kCGEventLeftMouseUp)
                        | CGEventMaskBit(kCGEventLeftMouseDown)
                        | CGEventMaskBit(kCGEventRightMouseUp)
                        | CGEventMaskBit(kCGEventRightMouseDown)
                        | CGEventMaskBit(kCGEventScrollWheel)
                        );
    
    eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, mask, CGEventCallback_r, nil);
    
    ...
}

And this is the callback function;这是回调 function;

CGEventRef CGEventCallback_r(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
    CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
    printf("Event Tap: %d\n", keycode);

    if (keycode == 43) {
        printf("quit the application \n");
        exit(EXIT_SUCCESS);
    }
    
    return nil;
}

What I want is, for example when the loop is running, if press "control + U + L", I want to run some function to exit the loop.我想要的是,例如当循环运行时,如果按“control + U + L”,我想运行一些 function 退出循环。 I believe in order to get the keyboard input, I should send the keyboard events as well but I couldn't figure out how to detect my keyboard shortcuts.我相信为了获得键盘输入,我也应该发送键盘事件,但我不知道如何检测我的键盘快捷键。

  • I added the keyboard bits to the mask.我将键盘位添加到掩码中。
  • The app is sandboxed.该应用程序是沙盒的。

I want to do something like this;我想做这样的事情;

CGEventRef CGEventCallback_r(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
    CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

    // When pressing "control + U + L", call a function
    if (...) {
        printf("pressed control + U + L \n");
        doSomething();
    }

    return nil;
}

I figured out like this;我是这样想的;

CGEventRef CGEventCallback_r(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
    
    if (type == kCGEventKeyDown) {
        CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
        
        if (CGEventGetFlags(event) & kCGEventFlagMaskControl) {
            // control key is pressed
            controlKeyPressed = true;
        }
        if (keyCode == kVK_ANSI_U) {
            // u key is pressed
            uKeyPressed = true;
        }
        if (keyCode == kVK_ANSI_L) {
            // l key is pressed
            lKeyPressed = true;
        }
        if (controlKeyPressed && uKeyPressed && lKeyPressed) {
            // doSomething();
        }
        return event;
    } else if (type == kCGEventKeyUp) {
        CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
        // set the variable to false just like above.
    }
    
    if (keycode == 43) {
        printf("quit the application \n");
        exit(EXIT_SUCCESS);
    }
    
    return nil;
}

Also, don't forget to put your variables on a global scope.另外,不要忘记将变量放在全局 scope 上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM