The subject should basically say it all. It’d be great if there was some way to register global hotkeys from JUCE. I’m trying to build an app roughly along the lines of Launchy/QS, but I can’t figure out a way to activate the app without some modification to JUCE.
Not sure how easy the OS would make that kind of thing, because obviously it’s not very secure to allow an app to spy on all your keystrokes. Maybe there are special functions to set up global hotkeys, but I’ve never looked into it.
It’s actually fairly easy on Windows, and I know it’s supported on OS X in some manner, though I haven’t looked at the implementation.
In Windows, you make a call to RegisterHotKey():
HWND hwnd = (HWND)getPeer()->getNativeHandle();
if(!RegisterHotKey(hwnd, 0x1538, MOD_ALT, VK_F7))
DWORD err = GetLastError();
When you type the user triggers the hotkey, a WM_HOTKEY message is posted to the hwnd window, with wparam = 0x1538 (some arbitrary identifier that you define in the call to RegisterHotKey()). I’ve inserted all of this code, and it seems to work. The only thing I’m missing is how to tell the component that something happened. It could probably be done in a way similar to how WM_APPCOMMAND messages are handled, but I’m not sure on the details.
I’ve often wondered if there’s even a way to insert your own responses to windows messages with juce, and I’ve always come to the conclusion that you can’t. I guess it’s not something that’s really practical on such a well cross-platform-ified library.
Conveniently enough though, yesterday I had to get my head back into using lower-level windows stuff, so I had some code ready for setting up a ‘window’ for dealing with messages.
After a quick tinker, I managed to get global hotkeys working, by registering KeyPress objects to respond to, and a HotKeyListener to do the appropriate business. It’s windows only though, but if you’re interested I can email it.
I’m not very concerned about cross platform support for this app, so yeah, I’d like to get a copy of that. Email: warmongerspam[at]charter.net
I’ll have to look at how to do the same thing in OS X, but most of my experience there is with Cocoa, so I’m not too sure what to expect.
edit: RegisterEventHotKey() appears to be the equivalent, and it takes fairly similar arguments.
i don’t know if they can be considered equivalents, but in X11 you can make use of the XGrabKey function, or eventually poll for XQueryKeymap changes.
Did anyone find out how to do this in OSX? System shortcuts would be a great new class for JUCE