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

标签 swift macos cocoa keydown

class func addGlobalMonitorForEvents(matching mask: NSEvent.EventTypeMask, handler block: @escaping (NSEvent) -> Void) -> Any?{
        print("this inside addGlobalMonitorForEvents")
    }
NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) {
        switch $0.modifierFlags.intersection(.deviceIndependentFlagsMask) {
                case [.shift]:
                    print("shift key is pressed")
                    print("key code is \($0.keyCode))")
                case [.control] where $0.characters == "1":
                    print("control key is pressed")
                    print("characters is \($0.characters)")
                case [.option] where $0.charactersIgnoringModifiers == "1" :
                    print("option key is pressed")
                    print("\($0.charactersIgnoringModifiers) is pressed")
                case [.command] where $0.keyCode == 19:
                    print("Command key is pressed")
                    print("\($0.keyCode) is pressed")
                case [.command, .shift] where $0.characters == "1":
                    print("command-shift keys are pressed")
                    print("\($0.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。同样的事情发生了。 我在这里错过了什么吗?

代码引用自here .虽然不确定 class func 做了什么,但当我按下按键时,我没有看到打印出“this inside addGlobalMonitorForEvents”。没有这个声明,它根本行不通。所以我还是添加了它。

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


更新

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

override func viewDidLoad(){
     super.viewDidLoad()
     NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
            self.keyDown(with: $0)
            return $0
        }
}

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 并检查隐私可访问性中的 xcodehelper(这是我认为唯一相关的一个,因为我在 xcode 中运行该应用程序),但不幸的是这没有帮助。

最佳答案

要在您的应用中启用 AXIsProcessTrusted(),您需要转到您的 macOS 系统首选项,打开安全和隐私,点击辅助功能 ,单击加号将您的应用程序添加到可以控制您的计算机的应用程序列表中。您可能需要单击挂锁并输入密码才能解锁设置。

enter image description here

import Cocoa

class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("AXIsProcessTrusted:",AXIsProcessTrusted())
        NSEvent.addGlobalMonitorForEvents(matching: .keyDown) {
            self.keyDown(with: $0)
        }
    }
    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
        }
    }
}

关于swift - 无法读取属性 'keyCode' 'character' 检测修改键+数字键时断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58386870/

相关文章:

objective-c - 如何在 NSOutlineView 中自动设置行高以适合文本?

objective-c - 来自@synthesize 的方法?

swift - 检查 UIWebView 是否在 Swift 中完成加载

ios - Swift:从 GameViewController 设置 GameScene 变量

ios - 如何根据 bool 值的结果运行 if else 函数

ios - 获取对象 C 中 NSRadioButton 中选定的单元格

objective-c - cocoa 结构之前预期的说明符限定符列表

ios - 阻止 UIButton 的 setTitle :forState: animation

macos - 如何在我的 visual studio 代码中使用 FSI for F# 4?

objective-c - fork Cocoa 进程并重新初始化 Cocoa。如何?