LoginSignup
10
9

More than 5 years have passed since last update.

Macアプリでホットキー設定機能を作る

Posted at

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];
}
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9