无法读取 属性 'keyCode' 'character' 检测修改键 + 数字键时断言失败

Cannot read property 'keyCode' 'character' Assertion failure when detect modifier key + numeric key

class func addGlobalMonitorForEvents(matching mask: NSEvent.EventTypeMask, handler block: @escaping (NSEvent) -> Void) -> Any?{
        print("this inside addGlobalMonitorForEvents")
    }
NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) {
        switch [=14=].modifierFlags.intersection(.deviceIndependentFlagsMask) {
                case [.shift]:
                    print("shift key is pressed")
                    print("key code is \([=14=].keyCode))")
                case [.control] where [=14=].characters == "1":
                    print("control key is pressed")
                    print("characters is \([=14=].characters)")
                case [.option] where [=14=].charactersIgnoringModifiers == "1" :
                    print("option key is pressed")
                    print("\([=14=].charactersIgnoringModifiers) is pressed")
                case [.command] where [=14=].keyCode == 19:
                    print("Command key is pressed")
                    print("\([=14=].keyCode) is pressed")
                case [.command, .shift] where [=14=].characters == "1":
                    print("command-shift keys are pressed")
                    print("\([=14=].characters) are pressed")
                default:
                    print("no modifier keys are pressed)")
                }
}

没有数字键,一切正常。只要我按 shift / command / option1.它抛出异常。我尝试通过 keyCode 字符和 charactersIgnoringModifiers 检查数字。所有这些要么给我一个断言失败 Assertion failure in -[NSEvent characters]Assertion failure in -[NSEvent charactersIgnoringModifiers] 或没有失败,而是打印修饰键本身的 keyCode 56 或其他东西。 我尝试了 chain .contains(.command) 然后打开 keyCode。同样的事情发生了。 我在这里错过了什么吗?

代码参考自。虽然不确定 class func 做了什么,但我在按键时没有看到 "this inside addGlobalMonitorForEvents" 打印出来。没有这个声明,它根本行不通。所以我还是添加了它。

如有任何建议或解释,我们将不胜感激。谢谢


更新

第一个代码片段 class 函数声明不是必需的。 现在可以监控 command+shift +1 的本地事件,但不能监控全局事件

override func viewDidLoad(){
     super.viewDidLoad()
     NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
            self.keyDown(with: [=15=])
            return [=15=]
        }
}

override func keyDown(with event: NSEvent) {
        switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
        case [.command] where event.characters == "1", [.command, .shift] where event.characters == "1":
            print("command+1 or command+shift+1")
        default:
            break
}

然后我更改为 NSEvent.addGlobalMonitorForEvents 并在隐私可访问性中检查 xcode 助手(这是我认为相关的唯一一个,因为我 运行 xcode 中的应用程序) ,但不幸的是,这没有帮助。

要在您的应用中启用 AXIsProcessTrusted(),您需要转到 macOS 系统偏好设置,打开 Security and Privacy,点击 Accessibility,点击加号将您的应用添加到可以控制您的计算机的应用程序列表。您可能需要单击挂锁并输入密码才能解锁设置。

import Cocoa

class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("AXIsProcessTrusted:",AXIsProcessTrusted())
        NSEvent.addGlobalMonitorForEvents(matching: .keyDown) {
            self.keyDown(with: [=10=])
        }
    }
    override func keyDown(with event: NSEvent) {
        switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
        case [.command] where event.characters == "1", [.command, .shift] where event.characters == "1":
            print("command+1 or command+shift+1")
        default:
            break
        }
    }
}