views:

458

answers:

1

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?

+3  A: 

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.

Peter Hosey
I see, how would you tell the SRRecorderControll to translate the modifier flags from Cocoa to Carbon?
Joshua
By using the method it has for that purpose.
Peter Hosey
Ah Ok, so is this part of the code the Keycombo '_KeyCombo { unsigned int flags; // 0 for no flags signed short code; // -1 for no code} KeyCombo;'? How would I use the modifier flags and virtual-key code in the Hot Key?
Joshua
That's the declaration of KeyCombo. It's in the header. You wouldn't include it again in your code; you'd just import the header. As for what to do with the key-code and (converted) modifier flags: Read the documentation for `RegisterEventHotKey`.
Peter Hosey
I can't see what In the Documentation would help. Sorry but i am not very good as this. What do a need to add or change to my current code to make this work?
Joshua
In the delegate method for the Shortcut Recorder, where it tells you the new key combo, put those values into User Defaults under the same keys you're using to retrieve them in the call to RegisterEventHotKey.
Peter Hosey
Ok, Was the Key Combo the code I posted earlier? Also, How would I put those values into User Defaults?
Joshua
Like I said, be the SRRecorderControl's delegate and implement the method for it to tell you the KeyCombo. Both the delegate property and the delegate methods are declared in SRRecorderControl's header. As for putting things into User Defaults: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/doc/uid/20000318-SW4
Peter Hosey
I see. So do I need to add some code from the Documentation to put things into the User Defaults or is there already some code there which I can edit to make work?
Joshua
You need to write some original code. You can't just copy code from other places and tape it together into an application; you need to write code that came from your own brain through your own hands. You must write your own implementation of the SRRecorderControl delegate method, which will use one of the methods named in the NSUserDefaults documentation to put the value into user defaults.
Peter Hosey
How would I write a delegate Method?
Joshua
OK, you seriously need to catch up on your Cocoa documentation reading. Start with http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ , then read http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/ , then http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/ , then http://developer.apple.com/documentation/Cocoa/Conceptual/AppArchitecture/ , and in fact about half the documents on http://developer.apple.com/documentation/Cocoa/index-title0.html .
Peter Hosey
Ok, I now have an idea what to do. I need to use a method from the User Defaults Doc, but which one should I use for setting a Default?
Joshua
They're all pretty clearly labeled.
Peter Hosey
Ok, but which do I need to set, Bool, Float, Integer or Object?
Joshua
Look in the declaration of KeyCombo and see which one would be most appropriate for the types of its members.
Peter Hosey
Would It Be Integer?
Joshua
Look in the declaration of KeyCombo. ⌘-double-click anything you don't recognize. Sooner or later, you'll get down to either a primitive type or an object type, and then you'll know which method to use for that member.
Peter Hosey
Where is the Declaration of KeyCombo is it in SRCommon.h?
Joshua
Also doing ⌘-double-click just highlights the text.
Joshua
Read SRCommon.h and find out. ⌘-double-click only works in Xcode (and if it doesn't work, you probably haven't added the framework in the project).
Peter Hosey
From what I can see i think it is still integer, if not can tell me what it actually is please.
Joshua
I am pretty sure It is integer, is this true or is it Bool, Float or Object?
Joshua
Also, once I know what method to use what do i put in the method?
Joshua
Did you forget what you wanted to do? Read your own comments above. You wanted to put the key combo into user defaults. That's what you put in the delegate method.
Peter Hosey
Oh Yeh, Are these this part of the KeyCombo http://snapplr.com/2kbw ?
Joshua
Those are glyphs, not keycode constants.
Peter Hosey
Ah, I think this the KeyCombo http://snapplr.com/z1th , am I right?
Joshua
That's a function to make a KeyCombo. Did you forget about ⌘-double-click already?
Peter Hosey
Peter Hosey
Or the slightly less intimidating Learn C on the Mac http://www.amazon.co.uk/gp/product/1430218096
Abizern
I will definitely look onto getting one of those books probably Learn C on the Mac. Also, is the Key Combo in the SRCommon.h file and if so is this the key combo http://snapplr.com/kkwg?
Joshua
Like I said, ⌘-double-click on the word `KeyCombo` in Xcode, and it will take you to the type definition.
Peter Hosey
So, is this the Key Combo http://snapplr.com/5dwh ?
Joshua
I am pretty sure that is the Key Combo as I ⌘-double-clicked on the word KeyCombo in Xcode and that is what came up.
Joshua
Um, its name is not `KeyCombo` but `kSRListenKeyCombo`. That would seem to disqualify it from being **`KeyCombo`**. I suggest reporting a bug in RadarWeb if Xcode took you there.
Peter Hosey
Oh, I will report the Bug to RadarWeb but how will I find the type definition now?
Joshua
I looked in the file where it took me when i ⌘-double-clicked KeyCombo and found that http://snapplr.com/xwmb line was the only line with the words KeyCombo.
Joshua
Not words. Word. It's a single word, not part of another word.
Peter Hosey
Ok, with the Word KeyCombo, but how will i find the type definition for KeyCombo?
Joshua
Look at one of your earlier comments. You pasted it.
Peter Hosey
So do i need to paste '_KeyCombo { unsigned int flags; // 0 for no flags signed short code; // -1 for no code } KeyCombo;' into the User Default method?
Joshua
I also had a go pasting the earlier code which came up when I ⌘-double-clicked KeyCombo it doesn't seem to come up with any errors except for the Method, Here is a Picture http://snapplr.com/65fq . Do I need to add something to the method to get rid of the Errors?
Joshua
“So do i need to paste [the type definition] into the User Default method?” Why would you need to do that?
Peter Hosey
As for that error: Read that code back to me and tell me what it does.
Peter Hosey
It sets an integer for a Key, do i need to put something after forKey:?
Joshua
Do I need to put something after forKey:?
Joshua
Just ordered Learn C On Mac, so hopefully with your help and the Book I should be able to figure this out.
Joshua
Can you tell me why I get those errors?
Joshua
[ END COMMENTS ]
Joshua