本地NSEvent Monitor产生不正确的键码

时间:2019-02-01 23:02:58

标签: macos appkit keycode nsevent

我正在使用本地监视器来观察关键事件。但是,当修饰符⌥和⌘处于活动状态并且同时按下具有代码42的键时,我得到了错误的键码。事件中的keyCode为8,在我的键盘上为C。事件监视器用于NSTextField的子类中,以拦截关键事件。

这是到目前为止,我得到的密码不正确的唯一情况。我也尝试过使用CGEventTap重现此问题,但使用CGEventTap时keyCode总是正确的。

有人经历过吗?这是一个已知的错误吗?

import Foundation import Cocoa

class FooTextField : NSTextField {

func initMonitor() {
    NSEvent.addLocalMonitorForEvents(matching: [.keyUp, .keyDown, .flagsChanged], handler: onKeyEvent)
}

private func onKeyEvent(_ e: NSEvent) -> NSEvent? {
    //prints 8 when key with keycode 42 is pressed and (⌥,⌘) are active.
    //the textfield seems to receives the correct keycode since a # appears for German keyboards
    print(e.keyCode)
    return e
}

}

0 个答案:

没有答案