我可以使用Shortcut Recorder使用此代码吗?

时间:2009-07-06 15:28:16

标签: objective-c cocoa macos-carbon

我有这个代码(如下)来创建一个可自定义的热键。

OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent,void *userData)
{
    EventHotKeyID hkCom;
    GetEventParameter(theEvent,kEventParamDirectObject,typeEventHotKeyID,NULL,sizeof(hkCom),NULL,&hkCom);
    HotKeyController *controller = (HotKeyController *)userData;
    int l = hkCom.id;
    switch (l) {
        case 1: [controller->window makeKeyAndOrderFront:NSApp];  
            break;
        case 2: [controller->searchWindow makeKeyAndOrderFront:nil];
            break;
        case 3: [controller->entryWindow makeKeyAndOrderFront:nil];
            break;  
    }
    return noErr;
}

- (void)awakeFromNib
{
    //Register the Hotkeys
    EventHotKeyRef gMyHotKeyRef;
    EventHotKeyID gMyHotKeyID;
    EventTypeSpec eventType;
    eventType.eventClass=kEventClassKeyboard;
    eventType.eventKind=kEventHotKeyPressed;


    InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,(void *)self,NULL);

    gMyHotKeyID.signature='htk1';
    gMyHotKeyID.id=1;
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeMain"]!=-999) {
        RegisterEventHotKey([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeMain"], [[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyModifiersMain"], gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
    }

    gMyHotKeyID.signature='htk2';
    gMyHotKeyID.id=2;
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeSearch"]!=-999) {
        RegisterEventHotKey([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeSearch"], [[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyModifiersSearch"], gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
    }

    gMyHotKeyID.signature='htk3';
    gMyHotKeyID.id=3;
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeEntry"]!=-999) {
        RegisterEventHotKey([[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyCodeEntry"], [[NSUserDefaults standardUserDefaults] integerForKey:@"hotkeyModifiersEntry"], gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
    }

}

但代码是通过弹出框制作Hotkey Customizable的,我如何制作它以便它可以与Shortcut Recorders Button Or Field.一起使用他们的示例应用程序(来自制作代码的人) )他们使用弹出框通过动作连接到控制器以选择热键和连接到NSUserDefaultsController的文本字段来显示它。我如何使快捷方式记录器字段/按钮选择热键,因为此时我将操作连接到用户驱动控制器,但它不起作用(即使热键工作)。如何更改代码以使其工作或使其执行应该执行的操作?

1 个答案:

答案 0 :(得分:3)

您需要从SRRecorderControl获取KeyCombo。它有修饰符标志和虚拟键码,您将在Carbon Event热键中使用它们。在将它们传递给RegisterEventHotKey之前,不要忘记告诉SRRecorderControl将修饰符标志从Cocoa转换为Carbon。

您可以在SRRecorderControl和SRCommon标题中找到所有这些内容。