DDHotKey in Xcode 7

159 views Asked by At

I am developing a toolbar app where I want to have a global shortcut. I found that this capability is only possible through use of Carbon event management and fortunately I found a Cocoa wrapper (DDHotKey) for this purpose. However, I can not get it to work. Here what I'm doing:

DDHotKeyCenter * center = [[DDHotKeyCenter alloc] init];
DDHotKey *key= [c registerHotKeyWithKeyCode:1 modifierFlags:NSControlKeyMask target:self action:@selector(print) object:nil];

After this code, key variable is nil which indicates that something went wrong. I also tried to debug and found that registerHotKeyWithKeyCode is not even called - breakpoint in its implementation is not triggered.

Need to mention that I included Carbon framework in the project settings.

DDHotKey documentation is not very comprehensive, so I stuck at this point.

1

There are 1 answers

0
Toby On BEST ANSWER

As it seems, DDHotKeyCenter is meant to be accessed through the singleton method +[DDHotKeyCenter sharedHotKeyCenter]. Since you are trying to create a new instance using alloc/init, nil is returned.

The reason can be found in this method +[DDHotkeyCenter allocWithZone:]:

+ (id)allocWithZone:(NSZone *)zone {
    return sharedHotKeyCenter;
}

+allocWithZone returns an instance variable that is only initialized in the +sharedHotkeyCenter method.