我需要检测用户按下的所有键。使用-keyDown:
可以获得大多数按键(字母数字键,功能键,箭头,空格键,转义键,返回键),但是当单独按下它时,我无法获得任何修饰键。
如何检测绝对的任何击键,包括修饰键?
最佳答案
尝试一下:
[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;
}];