-shortcutRecorder:keyComboDidChange: isn’t executed

268 views Asked by At

I am writing a Cocoa application and I'd like to implement a global hotkey function. I implemented the ShortcutRecorder.framework from Waffle Software and I added a customView to my xib. Then I subclassed the CustomView to SRRecorderControl. Now I see the Recorder in my Window, but how can I get the KeyCombo and how can I react on that?

I implemented the keyComboDidChange method with no luck to get the keycode. What am I doing wrong?

Here is my code for getting the keycode:

- (void)shortcutRecorder:(SRRecorderControl *)aRecorder keyComboDidChange:(KeyCombo)newKeyCombo
{
    if (aRecorder == shortcutRecorder)
    {
        NSLog{"We got a new Key Combo");
    }
}

shortcutrecorder is my IBOutlet btw.

Do I have to implement a protocol or setDelegate:self or something like that?

Edited to add

Actually I have declared my shortcutRecorder outlet in my Preferences.h. Then in the Identity Inspector I put "Preferences" as Custom Class for Files Owner in and I connect the delegate to my Shortcut Recorder... but the keyComboDidChange is never called ... I don't understand why.

2

There are 2 answers

3
Rick Pastoor On BEST ANSWER

Let me explain the steps I took to get it working:

  • Create a window xib
  • The file's owner of this class is, in my case, PreferencesWindowController
  • Create a referencing outlet from the window to the file's owner, by right clicking the window and dragging it to the file's owner
  • Add the custom view to your window
  • You have to connect the delegate of the ShortcutRecorder to the "File's owner". To do this, right-click the SRRecorderControl and drag the delegate to the "File's owner" on your left.

After this: the ShortcutRecorder only records the hotkey and leaves it to you what to do with it. You need to use the PTHotKeyCenter (which is shipped with ShortcutRecorder) or you could implement the shortcut handling yourself.

The ShortcutRecorder contains a great demo which demonstrates the use of the ShortcutRecorder in combination with the PTHotKeyCenter. It works like this:

  • Listen to events from the ShortcutRecorder (which you already do, but without setting the delegate)
  • Check if the globalHotKey variable is set
  • If so, unload the previous hotkey
  • Init a new hotkey with the settings from the ShortcutRecorder
  • Set the target and action to actually capture the hotkey, once pressed
  • Save the hotkey to the shared center (from this moment on, the hotkey will work)

Little sample, from their source:

if (globalHotKey != nil)
{
    [[PTHotKeyCenter sharedCenter] unregisterHotKey: globalHotKey];
    [globalHotKey release];
    globalHotKey = nil;
}

globalHotKey = [[PTHotKey alloc] initWithIdentifier:@"SRTest"
                                           keyCombo:[PTKeyCombo keyComboWithKeyCode:[shortcutRecorder keyCombo].code
                                                                          modifiers:[shortcutRecorder cocoaToCarbonFlags: [shortcutRecorder keyCombo].flags]]];

[globalHotKey setTarget: self];
[globalHotKey setAction: @selector(hitHotKey:)];

[[PTHotKeyCenter sharedCenter] registerHotKey: globalHotKey];

The only thing left to do is the hotkey handler:

- (void)hitHotKey:(PTHotKey *)hotKey
{
    NSLog(@"Hotkey pressed!");
}

You could easily save the hotkey settings to the UserDefaults to load them every time you application starts.

2
dehlen On

Actually I have declared my shortcutRecorder outlet in my Preferences.h. Then in the Identity Inspector I put "Preferences" as Custom Class for Files Owner in and I connect the delegate to my Shortcut Recorder... but the keyComboDidChange is never called ... I don't understand why -.-