5

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:

  1. How do I give this app full privileges (give accessibility permissions)?
  2. How do I show a pop-up asking the user to grant accessibility permissions if they are not enabled?
pkamb
  • 33,281
  • 23
  • 160
  • 191
  • https://stackoverflow.com/questions/17693408/enable-access-for-assistive-devices-programmatically-on-10-9 – pkamb Dec 02 '19 at 23:13
  • Duplicate of [How to grant Accessibilty access in Xcode](https://stackoverflow.com/questions/58675555/how-to-grant-accessibilty-access-in-xcode) – Willeke Dec 03 '19 at 01:19
  • 2
    I tried the suggestions in the related question. I however still do not see any pop-up or the option to add my app to the list of trusted apps for accessibility access. – Venkatesh Potluri Dec 03 '19 at 17:54

0 Answers0