アプリケーションがアクティブでない時でもホットキーで特定のアクションを実行させたい場合, https://github.com/davedelong/DDHotKey を使うとすぐ実装できる.使い方は以下.
DDHotKeyCenter *ddhkc = [[DDHotKeyCenter alloc] init];
if (![ddhkc registerHotKeyWithKeyCode:40 modifierFlags:(NSShiftKeyMask|NSCommandKeyMask) target:self action:@selector(someMethod:) object:nil]) {
NSLog(@"Failed to register hotkey");
}
このコードのうち
- キーコード
- モディファイヤキー
- アクション
- 渡すオブジェクト
の4つを変更し,これを例えばAppDelegate.m
の- applicationDidFinishLaunching:aNotification
などで実行してやればよい.
アプリをアクティブにしたいときは呼ばれるアクションの中で
[NSApp activateIgnoringOtherApps:YES]
を実行する.