Search code examples
macoseventsswift4

macOS Swift monitoring global events not working for generic keyDown


I did set the necessary privileges for the app, and for some events it is working.

For instance:

NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { print("\($0)") }

works, but

NSEvent.addGlobalMonitorForEvents(matching: .keyDown) { print("\($0)") }

doesn't.

So I am wondering, is it possible at all to listen globally for any keypress?


Solution

  • After checking the privileges via

    let accessEnabled = AXIsProcessTrustedWithOptions( [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)

    I found out, that due to the program running through the Xcode debugger granting the privileges to the app was not enough, I also had to give the privileges to Xcode itself.