Swift 检测修饰符键+数字键时无法读取属性“keyCode”“character”断言失败

Swift 检测修饰符键+数字键时无法读取属性“keyCode”“character”断言失败,swift,macos,cocoa,keydown,Swift,Macos,Cocoa,Keydown,没有数字键,一切都很好。只要我按shift/command/option和1。它抛出了一个例外。我试着用keyCode字符和characterSigningModifiers检查数字。它们要么在-[NSEvent characters]中给我一个断言失败,要么在-[NSEvent charactersIgnoringModifiers]中给我一个断言失败,要么不是失败,而是打印修改器键本身的键代码56或其他东西。 我尝试了chain.contains.command,然后打开了keyCode。发

没有数字键,一切都很好。只要我按shift/command/option和1。它抛出了一个例外。我试着用keyCode字符和characterSigningModifiers检查数字。它们要么在-[NSEvent characters]中给我一个断言失败,要么在-[NSEvent charactersIgnoringModifiers]中给我一个断言失败,要么不是失败,而是打印修改器键本身的键代码56或其他东西。 我尝试了chain.contains.command,然后打开了keyCode。发生了同样的事情。 我错过什么了吗

代码是从中引用的。虽然不确定func类做了什么,但当我按键时,我没有看到this inside addGlobalMonitorForEvents打印出来。如果没有这一声明,它根本不起作用。所以我还是加了

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

更新

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

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)")
                }
}
然后我改为NSEvent.addGlobalMonitorForEvents并在隐私访问性中检查xcodehelper这是我认为唯一相关的,因为我在xcode中运行应用程序,但不幸的是,这没有帮助。

要在应用程序中启用AXIsProcessTrusted,您需要转到macOS系统首选项,打开安全性和隐私,单击可访问性,单击加号可将您的应用程序添加到可控制计算机的应用程序列表中。您可能需要单击挂锁并键入密码来解锁设置

要在应用程序中启用AXIsProcessTrusted,您需要转到macOS系统首选项,打开安全和隐私,单击可访问性,单击加号将应用程序添加到可控制计算机的应用程序列表中。您可能需要单击挂锁并键入密码来解锁设置


您不必将此方法添加到代码中。addGlobalMonitorForEvents是NSEvent的类方法。顺便说一句,您不能全局监视键,只能监视修饰符。**只有在启用了可访问性或您的应用程序受信任可访问性时,才能监视与密钥相关的事件请参阅AXIsProcessTrusted**请阅读字符文档:此属性仅对向上键和向下键事件有效。如果在任何其他类型的事件对象上访问,则会引发NSInternalInconsistencyException。你想做什么?@Willeke试图在按下command+shift+1时触发一个函数。你是对的。我不应该读flagChanges事件中的字符。现在我可以监视本地command+shift+1事件,但不能监视全局事件。@LeoDabus我检查了您以前的一个事件,并稍微更新了我的问题。现在的问题是如何启用可访问性或以编程方式设置与AXIsProcessTrusted相关的值。在xcode中运行我的应用程序时,在可访问性中找不到它。我尝试在可访问性中启用Xcode助手,但没有帮助。您不必将此方法添加到代码中。addGlobalMonitorForEvents是NSEvent的类方法。顺便说一句,您不能全局监视键,只能监视修饰符。**只有在启用了可访问性或您的应用程序受信任可访问性时,才能监视与密钥相关的事件请参阅AXIsProcessTrusted**请阅读字符文档:此属性仅对向上键和向下键事件有效。如果在任何其他类型的事件对象上访问,则会引发NSInternalInconsistencyException。你想做什么?@Willeke试图在按下command+shift+1时触发一个函数。你是对的。我不应该读flagChanges事件中的字符。现在我可以监视本地command+shift+1事件,但不能监视全局事件。@LeoDabus我检查了您以前的一个事件,并稍微更新了我的问题。现在的问题是如何启用可访问性或以编程方式设置与AXIsProcessTrusted相关的值。在xcode中运行我的应用程序时,在可访问性中找不到它。我尝试在可访问性中启用Xcode助手,但没有帮助。谢谢。那真的很有帮助。虽然我花了一段时间说服自己在实际应用程序上测试,但它甚至不在xcode中工作。我努力想找到一种方法来证明它的有效性,因为我看不出它将信息打印到哪里。我最终通过选择等待可执行文件在scheme中启动来证明这一点。你知道我怎样才能很容易地看到打印出来的内容吗?最后一个问题是,我实际上想要监视器cmd+shift+4,这是截图。但当我按下组合键时,它没有击中案例分支。我们应该如何应对这种情况?@OscarZhang我想这些都是保留意见。即使您在键盘快捷键屏幕截图首选项中禁用它们。谢谢Leo。那真的很有帮助。虽然我花了一段时间说服自己在实际应用程序上测试,但它甚至不在xcode中工作。我努力想找到一种方法来证明它是有效的,但我做不到
查看它将消息打印到哪里。我最终通过选择等待可执行文件在scheme中启动来证明这一点。你知道我怎样才能很容易地看到打印出来的内容吗?最后一个问题是,我实际上想要监视器cmd+shift+4,这是截图。但当我按下组合键时,它没有击中案例分支。我们应该如何应对这种情况?@OscarZhang我想这些都是保留意见。即使在键盘快捷键屏幕截图首选项中禁用它们。
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)")
                }
}
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
}
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
        }
    }
}