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

后端 未结 1 510
臣服心动
臣服心动 2020-12-22 02:09
class func addGlobalMonitorForEvents(matching mask: NSEvent.EventTypeMask, handler block: @escaping (NSEvent) -> Void) -> Any?{
        print(\"this inside add         


        
相关标签:
1条回答
  • 2020-12-22 02:53

    To enable AXIsProcessTrusted() in your app you need to go to your macOS system preferences, open Security and Privacy, click on Accessibility, click on the plus sign to add your app to the list off apps that can control your computer. You might need to click at the padlock and type your password to unlock your settings.

    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
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题