How to intercept a hot key in Cocoa when the application window is not active

我是研究僧i 提交于 2019-12-03 04:38:56

问题


I am trying to create a utility that doesn't open a window when executed, and that would be activated from a hot key; I read that currently Cocoa doesn't have a function for that, and that I should use a deprecated Carbon function.

Isn't there really a way to use global hot keys in Cocoa? What should I do: wait for Cocoa to introduce a function for that, or use the carbon function until a similar function is not introduced in Cocoa?


回答1:


Use the Carbon Event Manager's RegisterEventHotKey function. This function is supported in 64-bit (notice that it lacks the “not available in 64-bit” availability note).

Conversely, NSEvent's new addGlobalMonitorForEventsMatchingMask:handler: method in Snow Leopard is not the easiest way to implement a hot-key. For one thing, it requires that the user have access for assistive devices turned on; moreover, it requires you to examine every event yourself, as compared with the RegisterEventHotKey system, which only calls your callback function when the user presses the specific key you registered for.




回答2:


Take a look at Shortcut Recorder (http://wafflesoftware.net/shortcut/) a framework that uses the Carbon Event Manager for Global Hotkeys it also allows you to change the user to change the HotKey (if this is what you want).

And see this Project/Code on how to implement it : http://github.com/sdegutis/SDGlobalShortcuts.



来源:https://stackoverflow.com/questions/1914811/how-to-intercept-a-hot-key-in-cocoa-when-the-application-window-is-not-active

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