I had used addLocal
and it would work in my application, but I want to watch globally so i tried addGlobalMonitorForEventsMatchingMask:handler:
but NSEventKeyDown
is not getting triggered at all. Apparently it needs assistive permission enabled.
I am able to watch NSScrollWheel events globally. Which is interesting.
Before I put a bunch of time into and come to same roadblock I was hoping to ask first, would using CGEventTap
to monitor key events work globally without assitive turned on?
I will also explore other methods but was very curious (for learning) about if CGEventTap will work globally no problems. He seems to hint here that it may work but the comments show some confusion: https://stackoverflow.com/a/9345536/1828637
Other methods I am going to use also Carbon APIs.
A CGEventTap
requires the same access for assistive devices permissions as a global event monitor. From the documentation for CGEventTapCreate()
:
Event taps receive key up and key down events if one of the following conditions is true:
The current process is running as the root user.
Access for assistive devices is enabled. In OS X v10.4, you can enable this feature using System Preferences, Universal Access panel, Keyboard view.
I recommend using the old Carbon RegisterEventHotKey()
mechanism. It still isn't deprecated and is available for 64-bit. This is in contrast to much of Carbon. Basically, Apple recognizes that there's no proper replacement, yet.