How to listen to global hotkeys with Swift in an OS X app?

前端 未结 6 1752
北荒
北荒 2020-12-12 17:08

I\'m trying to have a handler in my Mac OS X app written in Swift for a global (system-wide) hotkey combo but I just cannot find proper documentation for it. I\'ve read that

6条回答
  •  -上瘾入骨i
    2020-12-12 17:53

    A quick Swift 3 update for the setup:

    let opts = NSDictionary(object: kCFBooleanTrue, forKey: kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString) as CFDictionary
    
    guard AXIsProcessTrustedWithOptions(opts) == true else { return }
    
    NSEvent.addGlobalMonitorForEvents(matching: .keyDown, handler: self.handler)
    

提交回复
热议问题