【问题标题】:Detect any key press, including modifier keys, on OS X检测 OS X 上的任何按键,包括修改键
【发布时间】:2014-05-18 04:49:01
【问题描述】:

我需要检测用户按下的所有键。使用-keyDown:,我可以获得大多数按键(字母数字、功能键、箭头、空格键、转义、返回),但单独按下时我无法获得任何修饰键。

如何绝对检测到任何击键,包括修饰键?

【问题讨论】:

    标签: macos cocoa keyboard keypress nsevent


    【解决方案1】:

    flagsChanged: 方法可用于检测修改键的按下而不同时按下任何其他键。例如,如果用户自己按下 Option 键,您的响应者对象可以在其 flagsChanged: 的实现中检测到这一点。

    【讨论】:

    • 有趣,但我收到两个事件,按键按下和按键按下。有什么方法可以判断是哪一个?
    • 您保存上一个通知中modifierFlags 的副本,并将其与当前的modifierFlags 进行比较,以告知您修改键是刚刚添加到标志中,还是刚刚删除。
    【解决方案2】:

    试试看:

    [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;
    }];
    

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2011-12-27
      • 1970-01-01
      • 2016-03-27
      • 2012-09-14
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多