Mac: 注册全局热键

1 min read Apr 20, 2014

Mac: 注册全局热键

使用 Cocoa 为应用注册热键, 首先需要引入 Carbon.framework, 并包含头文件。

#import <Carbon/Carbon.h>

接下来, 要做两件事, 注册热键、设置回调。

回调设置:

OSStatus MXHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData) {
    // ... callback
    return noErr;
}

注册热键:

- (void)registerKey {
    EventHotKeyRef       gMyHotKeyRef;
    EventHotKeyID        gMyHotKeyID;
    EventTypeSpec        eventType;

    eventType.eventClass = kEventClassKeyboard;
    eventType.eventKind = kEventHotKeyPressed;

    InstallApplicationEventHandler(&MXHotKeyHandler, 1, &eventType, NULL, NULL);

    gMyHotKeyID.signature = 'capk';
    gMyHotKeyID.id = 1;

    // cmd + shift + x
    RegisterEventHotKey(7, cmdKey + shiftKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
}

上面的示例中, 我们注册了 ⌘ + ⇧ + X 为热键。X 的代码为 7, 其它键码可以在 Carbon/Frameworks/HlToolbox/Events.h 中查询。

Picsew | Ezra