Detect any key press, including modifier keys, on OS X

前端 未结 2 1168
悲&欢浪女
悲&欢浪女 2020-12-12 00:46

I need to detect all the keys that the user presses. Using -keyDown: I can get most key presses (alphanumeric, function keys, arrows, space bar, escape, return)

相关标签:
2条回答
  • 2020-12-12 01:41

    Try it:

    [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask|NSFlagsChangedMask handler:^NSEvent *(NSEvent *incomingEvent) {
        if (incomingEvent.type == NSFlagsChanged && (incomingEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) {
            NSLog(@"modifier key down");
        } else if (incomingEvent.type == NSKeyDown) {
            NSLog(@"other key down");
        }
    
        return incomingEvent;
    }];
    
    0 讨论(0)
  • 2020-12-12 01:46

    The flagsChanged: method can be useful for detecting the pressing of modifier keys without any other key being pressed simultaneously. For example, if the user presses the Option key by itself, your responder object can detect this in its implementation of flagsChanged:.

    0 讨论(0)
提交回复
热议问题