Question

I have this code(below) to create a customizable Hot Key.

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);
    }

}

But the code was made to make the Hotkey Customizable via a Popup Box, how would I make it so it would work with Shortcut Recorders Button Or Field. In their example application (from the person who made the code) they used a Popup box connected via an action to the Controller to Choose the Hotkey and a Text Field connected to a NSUserDefaultsController to Display it. How would I make the Shortcut Recorders Field/Button choose the Hotkey because at the moment i connect the action to and the User Deault Controller to it but it doesn't work (i.e Make the Hotkey Work) . How would the code need to be changed to make it work or make it do what is should do?

Was it helpful?

Solution

You'll need to get the KeyCombo from the SRRecorderControl. That has the modifier flags and virtual key-code, which you'll use in your Carbon Event hot-key. Don't forget to tell the SRRecorderControl to translate the modifier flags from Cocoa to Carbon before you pass them to RegisterEventHotKey.

You'll find all of these in the SRRecorderControl and SRCommon headers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top