Evernoteなどで使われているShortcutRecorderを使うのが良さそう.
バージョン
$ svn log -l 1
------------------------------------------------------------------------
r80 | chris@growl.info | 2012-04-24 03:42:21 +0900 (火, 24 4 2012) | 2 lines
Updating Greek and French
------------------------------------------------------------------------
ダウンロード
svn checkout http://shortcutrecorder.googlecode.com/svn/trunk/ shortcutrecorder-read-only
ビルド
ShortcutRecorder.xcodeproj
を開きArchiveする.
アーカイブ後になぜかOrganizerが立ち上がらないので,ビルドメッセージを頼りに~/Library/Developer/Xcode/DerivedData/ShortcutRecorder-XXXXXXX/ArchiveIntermediates/ShortcutRecorder.framework - with embedded ibplugin/Frameworks
とかにあるやつをコピーした.なぜだ…
ドキュメント
ない.ShortcutRecorder.h
を見ればだいたいわかるはず.
使い方
インターフェースビルダー
GettingStartedInSVNTrunkを参考にする.Xcode 4用のIB pluginはなさそう.
UIView(高さ22,幅は150以上ぐらいがよい)を適当な場所に置き,クラス名をSRRecorderControll
とする.適当にdelegateを設定する.
コード
変更を設定に保存するには以下のような感じ.実際にホットキーで何か処理するには,
アクティブでないアプリの特定アクションをホットキーで実行する - Qiitaで説明しているDDHotKeyCenterを使えばよい.
- (void)shortcutRecorder:(SRRecorderControl *)aRecorder keyComboDidChange:(KeyCombo)newKeyCombo {
unsigned short keyCode = newKeyCombo.code;
NSUInteger flags = newKeyCombo.flags;
// Register a hotkey
SEL selector = @selector(newItem:);
DDHotKeyCenter *ddhkc = [[DDHotKeyCenter alloc] init];
[ddhkc unregisterHotKeysWithTarget:[NSApp delegate] action:selector];
if (![ddhkc registerHotKeyWithKeyCode:keyCode modifierFlags:flags target:[NSApp delegate] action:selector object:nil]) {
NSLog(@"Failed to register the hotkey for new items");
}
// Save the keycombo to user settings
NSDictionary *keyComboDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedShort:keyCode], @"keyCode", [NSNumber numberWithUnsignedInteger:flags], @"flags", nil];
NSString *key = @"KeyCombo";
[[NSUserDefaults standardUserDefaults] setObject:keyComboDict forKey:key];
}