Global Keyboard Shortcut/Hot Key

Is there any way to have a keyboard shortcut that applies from any program?

Thanks.

It’s been a year. Yes or no?

Actually…
May 10, 2004 - Mar 10, 2005 = 10 months

Perhaps if you were a bit more friendly and less pushy… people would be more inclined to help. If you couldn’t figure this out for yourself in 10 months, you haven’t been looking very hard for answers. Don’t blame it on US that you don’t have an answer.

There are lots of launcher programs that can assign “universal” keyboard combinations. There is no feature built into the OS that allows you to add customizable keyboard shortcuts to ANY app that can execute YOUR code. Thus, there could be no method for an applescript to manipulate a feature that doesn’t exist.

As a workaround, you may be able to create a faceless background app with a hidden window that floats above all other windows, and contains a hidden button or menu that has a shortcut assigned to it. Haven’t tried this but it may work.

You could also create a status menu that sit’s in the menu bar, and assign its items keyboard shortcuts. This is a bit more involved, using lots of obj-c, and takes up menu bar space, so it may not be realistic for your purposes.

j

Sorry about that, I wasn’t trying to be pushy, I just happened to be looking for a similar thing that I had apparantly posted (almost) a year ago, and it was just kind of annoying that no one had responded after all that time.

Also, how do programs like Quicksilver and iSeek do things like this?

I don’t use either, so I can’t say for certain. iSeek appears to run as a status menu (or menu extra), which would be similar to the second method I mentioned previously. Since the menu is attached permanently to the main menu bar, it’s commands are always available to the user (assuming the finder is running). Quicksilver appears to be able to run using multiple interfaces. Perhaps they have three ui’s set up… one a status menu, one a dock menu, one a hidden window with a menu in it. The docs say the user can toggle between them, and if the status menu and dock are both disabled, there must be some other element active full-time that captures events for the app. It would be easy for them to programmatically create a menu, and then build the app in one of the mentioned configurations depending on how the user wanted to use the app.

Essentially, you need to find a way to make a menu or other control which can handle keyboard commands permanently available to the user. Be it in the menu bar, the dock, or a faceless window with a really high level. How you accomplish it depends largely on what you’re trying to do and how complex the task is. I’ll take a look tomorrow into whether the faceless window idea will even work, and let you know in a year or two what I find out. 8)

j

If you feel good with the menu-extra, you could take a look to Jon’s framework for system-wide menus (obj-c <-> AS):
http://homepage.mac.com/jonn8/as/html/MenuApp.html
But, as jobu said, if it is a little task, a menu-extra will overload your machine too much, and you may better use any of the available apps (eg, FastScripts).
If you downgrade to OS 9, I think Akua Sweets can install custom menus and global keystrokes, IIRC.

I looked further into this and have found the window approach (although a viable concept fundamentally) to be out of the scope of applescript studio projects. It is possible to float a window above all others, but the focus can still be taken away from it making it’s connections useless.

Basically, only carbon supports support for global key events, but code does exist which can add the functionality to cocoa apps. Unfortunately, the process is very complex and probably going to be beyond what is reasonable for you to do to add a single hot key. Unless you’re planning to learn cocoa in depth and write your own launcher application, you’re probably not even going to want to start down this path.

You can use a status menu or menu extra to do this, but cocoa will still be required to get the menu drawn and connections made. The disadvantage to menu extra’s is that apple dropped support for third-party ME’s and you need some other third-party software to enable them again. I think you may be biting off more than you can chew. :frowning:

j

You don’t need to do all of this. You can create an application that simply registers your key globally and runs faceless as a daemon listening for that key. When it does “hear” that key, do something. I used the code from Apple:

http://developer.apple.com/samplecode/iGetKeys/listing1.html

to create a demo AppleScript Studio application that registers a single global hotkey. When the hotkey is clicked (note, it always needs the Command key with whatever key you assign but you can also optionally require Control and/or Option), an AppleScript handler in the app is called. This app does have a window that is used to assign, register and unregister the hotkey but this could all be done in code and doesn’t really need a window open, just the app.

Right now it just displays a dialog when the hotkey is pressed but you could modify it to do what ever you want. The project is called “AS_HotKey” and can be downloaded from:

http://homepage.mac.com/jonn8/as/dist/AS_HotKey.zip

This project is provided “As Is” under the GPL with no support or documentation and very little in terms of commenting.

Good luck,
Jon

When you register the hot key, does that only apply for the time the application is running? Or is it some global system thing that you have to be careful with?

I’m not sure. I think you should unregister the hotkey before quitting the application but not doing so doesn’t seem to effect my machine and I can reregister the same hotkey if I relaunch without unregistering first. I’m sure there is a way to check the OSStatus from another app to see if the OS is listening for that key and doing anything when it’s pressed. The registered keys are linked to the unique identifier for the application (kMyHotKeyIdentifier). In this case, I chose “AShk” and when creating your own application, you should use your own unique ID and, ideally, it should be the same as the creator code that you register with Apple for your app (you are doing that, right? :wink: ), This code also only registers a single hotkey but you could register more. It’s been a while since I looked at it but I only need one at the time.

Jon

According to the Apple documentation, the hotkey doesn’t need to be unregister on quitting of the app, becasue the os takes care of it, the only time it needs unregistering is if you change the hotkey while the app is running, so you unregister you old hotkey and register a new one. Just incase anyone wonders…

P.S. John your example code is great, helped me loads.

Mark

Thanks for the code. I am using it in a project and I have changed the window to non-visible and changed the onClick clode to onLaunched so there is no user imput needed. That is all good but I can’t port it to my own application! I have tried making a new project and cop+paste all files put to no avail.
My last resort was to copy the original project, make the changes build it and the change the .plist file so that it is not called AS_HotKey.

Can anyone tell me the changes I need to make to do this?
Thanks :slight_smile: