Hotkey when no assistance enabled CGEventTap

匆匆过客 提交于 2019-12-12 00:15:35

问题


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?

https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/tdef/CGEventTapLocation

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.


回答1:


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.



来源:https://stackoverflow.com/questions/32299785/hotkey-when-no-assistance-enabled-cgeventtap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!