Cocoa NSStatusBar Global HotKey

2019-01-21 20:35发布

I have created an NSStatusBar cocoa application which sits in the system status bar.

I want to assign a hotkey so that when pressed it toggles my applications and show the menu.

Is this possible?, In my searching and experimenting I have found a few different ways of assigning global hot keys that can be pressed when your application is in the background but I can't find any way to problematically make the menu show.

Is this possible?, If anyone thinks a way of assigning a global hotkey is best please post it.

Thanks.


One of the hotkey tutorials I found was on http://dbachrach.com/blog/2005/11/program-global-hotkeys-in-cocoa-easily/ for anyone interested.

标签: cocoa hotkeys
2条回答
疯言疯语
2楼-- · 2019-01-21 21:13

There is an actual hotkey API, which still exists in Snow Leopard and is available in 64-bit. It's designed specifically for this purpose, unlike the NSEvent methods, which are essentially just a block-based wrapper around CGEventTaps.

The difference is that the NSEvent methods (or CGEventTaps directly) make you look at every event that comes in, whereas the hotkey API only calls your function when the user presses your hotkey.

查看更多
闹够了就滚
3楼-- · 2019-01-21 21:26

If you're targeting 10.6+, there's some new API for NSEvent that can do global hotkeys. For more information, check out this awesome blog post: http://cocoakids.net/global-hotkeys-in-cocoa-on-snow-leopard

EDIT (a long time later)

Tooting my own horn a bit: I could never get things like PTHotKey and other libraries to work the way I was expecting, so I eventually gave up and wrote my own HotKey wrapper. It has a very simple API (you give it a key code, modifiers, a target, and an action), that even supports fun things like 10.6's blocks. You can download the source here: http://github.com/davedelong/DDHotKey

查看更多
登录 后发表回答