Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa NSStatusBar Global HotKey

Tags:

cocoa

hotkeys

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.

like image 618
Craig Avatar asked Nov 01 '09 16:11

Craig


2 Answers

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

like image 141
Dave DeLong Avatar answered Sep 26 '22 01:09

Dave DeLong


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.

like image 22
Peter Hosey Avatar answered Sep 25 '22 01:09

Peter Hosey