ラベル Mac の投稿を表示しています。 すべての投稿を表示
ラベル Mac の投稿を表示しています。 すべての投稿を表示

2011年12月30日金曜日

ホットキーを登録する

ホットキーについて調べてみると Carbon フレームワークを使用して登録するというのが常套手段のようだ。

しかし、今更 Carbon か、、、という感は否めない。


そこで探してみると、 NSEvent のメソッドがあった。


Installs an event monitor that receives copies of events posted to other applications.
+ (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)mask
                                    handler:(void (^)(NSEvent*))block


Installs an event monitor that receives copies of events posted to this application before they are dispatched.
+ (id)addLocalMonitorForEventsMatchingMask:(NSEventMask)mask
                                   handler:(NSEvent* (^)(NSEvent*))block


addGlobalMonitorForEventsMatchingMask:handler: は
他のアプリケーションに対して送られたイベントを処理するためのメソッドで、
addLocalMonitorForEventsMatchingMask:handler: は
自アプリケーションに対して送られたイベントを処理するためのメソッドのようだ。

戻り値として、 eventMonitor オブジェクトが返却されるので不要になった際に必ず登録を解除してやる必要がある。
+ (void)removeMonitor:(id)eventMonitor

Carbon を使ったやり方だと NSApplication クラスのサブクラス化など必要で手間が多かったのだが
これなら簡単に実装できそうだ。

参考リンク
NSEvent Class Reference (Mac OS X Developer Library)