macos 如何在 Mac Cocoa App 中实现快捷键输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8201338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement shortcut key input in Mac Cocoa App?
提问by Victor Shcherbakov
I've needed to make a global hot key input box in my Cocoa App.
我需要在我的 Cocoa 应用程序中制作一个全局热键输入框。
I know about Shortcut Recorder, but it is a very old solution. It has parts implemented using Carbon, which has been deprecated, and I can't publish my app to the Mac App Store if I use it.
我知道 Shortcut Recorder,但它是一个非常古老的解决方案。它有使用 Carbon 实现的部分,已被弃用,如果我使用它,我无法将我的应用程序发布到 Mac App Store。
Is there any ready-to-use modern solution? Can anybody give me the way to make this by myself (I don't know where to start from)?
有没有现成的现代解决方案?谁能给我自己做这个的方法(我不知道从哪里开始)?
回答by Vadim
There is a modern framework named MASShortcutfor implementing Global Shortcuts in OS X 10.7+.
有一个名为MASShortcut的现代框架用于在 OS X 10.7+中实现全局快捷方式。
回答by kiamlaluno
In Mac OS X 10.6 and higher, you can use the methods +addGlobalMonitorForEventsMatchingMask:handler:and +addLocalMonitorForEventsMatchingMask:handler:defined from the NSEvent
class. Monitoring Eventsreports the following information:
在 Mac OS X 10.6 及更高版本中,您可以使用从类中定义的+addGlobalMonitorForEventsMatchingMask:handler:和+addLocalMonitorForEventsMatchingMask:handler: 方法NSEvent
。监控事件报告以下信息:
Local and global event monitors are mutually exclusive. For example, the global monitor does not observe the event stream of the application in which it is installed. The local event monitor only observes the event stream of its application. To monitor events from all applications, including the "current" application, you must install both event monitors.
本地和全局事件监视器是相互排斥的。例如,全局监视器不会观察安装它的应用程序的事件流。本地事件监视器只观察其应用程序的事件流。要监视来自所有应用程序(包括“当前”应用程序)的事件,您必须安装两个事件监视器。
The code shown in that page is for a local event monitor, but the code for a global event monitor is similar; what changes is the invoked NSEvent
's method.
该页面中显示的代码用于本地事件监视器,但全局事件监视器的代码类似;改变的是被调用NSEvent
的方法。
_eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:
(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask)
handler:^(NSEvent *incomingEvent) {
NSEvent *result = incomingEvent;
NSWindow *targetWindowForEvent = [incomingEvent window];
if (targetWindowForEvent != _window) {
[self _closeAndSendAction:NO];
} else if ([incomingEvent type] == NSKeyDown) {
if ([incomingEvent keyCode] == 53) {
// Escape
[self _closeAndSendAction:NO];
result = nil; // Don't process the event
} else if ([incomingEvent keyCode] == 36) {
// Enter
[self _closeAndSendAction:YES];
result = nil;
}
}
return result;
}];
Once the monitor is not anymore necessary, you remove it using the following code:
一旦不再需要监视器,您可以使用以下代码将其删除:
[NSEvent removeMonitor:_eventMonitor];
回答by Peter Hosey
Not all of Carbon is deprecated. You can't make a pure-Carbon application anymore, but some APIs live on and some of them are still the easiest way to do certain things.
并非所有 Carbon 都已弃用。你不能再制作纯碳应用程序,但有些 API 仍然存在,其中一些仍然是做某些事情的最简单方法。
One of these is the Carbon Events hotkey API. You certainly cansift through all the events using NSEvent's event-monitor methods, but it's unnecessary work. The Carbon Events hotkey API is still supported and much simpler—you just tell it what key you want to match and what function to call when the key is pressed. And there are Cocoa wrappers such as DDHotKey that make it even simpler.
其中之一是 Carbon Events 热键 API。您当然可以使用 NSEvent 的事件监视器方法筛选所有事件,但这是不必要的工作。Carbon Events 热键 API 仍然受支持且简单得多——您只需告诉它您想要匹配的键以及按下键时要调用的函数。还有一些 Cocoa 包装器,例如 DDHotKey,使它变得更加简单。
- RegisterEventHotKey, the relevant Carbon Events function (see also UnregisterEventHotKey in the same doc)
- DDHotKey
- SGHotKeysLib, another wrapper
- MASShortcut, yet another wrapper (suggested by TongG):
- RegisterEventHotKey,相关的 Carbon Events 函数(另请参阅同一文档中的 UnregisterEventHotKey)
- 热键
- SGHotKeysLib,另一个包装器
- MASShortcut,另一个包装器(由 TongG 建议):