Cocoa global shortcuts?

♀尐吖头ヾ 提交于 2019-12-20 09:38:38

问题


I want to create a global shortcut for my app. I've used the 'cool new way' of doing this with the addGlobalMonitorForEventsMatchingMask method. The problem is, my events don't get "consumed": my shortcut includes the spacebar, so whenever I use the shortcut, Quicklook pops up when I'm in the Finder.

How can I prevent this from happening? I know it's possible, because many apps, for example the Bowtie iTunes controller app, do it.


回答1:


addGlobalMonitorForEventsMatchingMask: is not a suitable replacement for the existing Carbon Hotkey API for all sorts of reasons; it's horribly inefficient, for one. And from the docs:

... you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application.

and

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access

The Hotkey API is still the only way to implement global shortcut in OS X. But unlike many Carbon APIs, the Hotkey API is available to 64-bit apps.




回答2:


Use the hotkey API, which is meant for this purpose.



来源:https://stackoverflow.com/questions/2135061/cocoa-global-shortcuts

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