当应用程序窗口不活动时如何拦截Cocoa中的热键
我正在尝试创建一个实用程序,该实用程序在执行时不会打开窗口,并且可以通过热键激活;我读到目前 Cocoa 没有相应的函数,我应该使用已弃用的 Carbon 函数。
难道Cocoa真的没有办法使用全局热键吗?我该怎么办:等待Cocoa为此引入一个函数,或者使用carbon函数直到Cocoa中没有引入类似的函数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Carbon 事件管理器的
RegisterEventHotKey
函数。 此函数在 64 位中受支持(请注意,它缺少“在 64 位中不可用”可用性说明)。相反,Snow Leopard 中 NSEvent 的新
addGlobalMonitorForEventsMatchingMask:handler:
方法不是实现热键的最简单方法。一方面,它要求用户能够访问打开的辅助设备;此外,它要求您自己检查每个事件,而RegisterEventHotKey
系统仅在用户按下您注册的特定键时调用您的回调函数。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 theRegisterEventHotKey
system, which only calls your callback function when the user presses the specific key you registered for.看一下 Shortcut Recorder (http://wafflesoftware.net/shortcut/),它是一个使用全局热键的 Carbon 事件管理器还允许您更改用户来更改热键(如果这是您想要的)。
并查看此项目/代码以了解如何实现它:http://github.com/sdegutis/SDGlobalShortcuts。
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.