I am trying to listen for global keyboard events and make my app do specific things when certain key presses are detected. to do this, I would like to use:
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Add keyboard event listener.
NSEvent.addGlobalMonitorForEvents(matching: .keyDown, handler:
{
print("key down!");
print($0.keyCode);
});
}
}
and I am checking for accessibility permissions in my appDelegate.swift this way:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
//check if the process is trusted for accessibility. This is important for us to listen for keyboard events system wide.
let checkOptPrompt = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString
let options = [checkOptPrompt: true]
let isAppTrusted = AXIsProcessTrustedWithOptions(options as CFDictionary?);
if(isAppTrusted != true)
{
print( "please allow accessibility API access to this app.");
}
}
I went ahead and granted Xcode accessibility permissions in system preferences. I however see the log statement asking to grant accessibility permissions to this app. simply put, here is my big question, divided into two smaller questions:
- How do I give this app full privileges (give accessibility permissions)?
- How do I show a pop-up asking the user to grant accessibility permissions if they are not enabled?