© 2010 Jason Frame [ [email protected] / @jaz303 ]
Released under the MIT License.
JFHotkeyManager is a small Cocoa convenience wrapper around Carbon's mechanism for implementing global hotkeys. It allows you to add hotkeys to your app with a couple of lines of code.
Copy JFHotkeyManager.h
and JFHotkeyManager.m
into your Xcode project
Hotkeys can be bound either by individually specifying keycode/modifiers, or symbolically using a string. The latter is more convenient but probably less reliable across international keyboards.
// Initialise a new hotkey manager
JFHotkeyManager *hkm = [[JFHotkeyManager alloc] init];
// Bind a hotkey by key code reference number and modifiers:
// (when hotkey is triggered, sends mySelector1 to self)
[hkm bindKeyRef:49
withModifiers:cmdKey + optionKey + shiftKey
target:self
action:@selector(mySelector1)];
// Bind a hotkey symbolically
// (when hotkey is triggered, sends mySelector2 to self)
[hkm bind:@"command shift up" target:self action:@selector(mySelector2)];
The bind methods return an opaque value of type JFHotKeyRef
that can be latter passed to unbind:
to unbind the hotkey.
Program Global Hotkeys in Cocoa Easily by Dustin Bachrach