views:

208

answers:

2

I'd like to create some kind of background process that listens to all keystrokes event and acts consequently (for example does some action if CMD-A is pressed while in Finder.app, or more complex things, like sequences to create shortcuts like in emacs..)

But how can I listen to keypresses systemwide on SnowLeopard? I thought of Applescript.. but found nothing..
(I'm good at ruby too, and intended to use rb-appscript actually)

thanks!

A: 

You can create a Service with Automator. If you want to run a ruby script, use the Run Shell Script action and select /usr/bin/ruby in the Shell: popup menu (no need for rb-appscript).

Services can be given a Keyboard Shortcut in the System Preferences -> Keyboard preference panel.

ghoppe
Not what I'm looking for, that's simply a system shortcut that executes a script.. I want a script that listens to keystrokes to bind even the most complex key sequences to commands.
luca
+2  A: 

If you really want to capture all the keystrokes system-wide, you need CGEventTap. I don't think it's available directly to AppleScript, maybe on 10.6 you can use AppleScriptObjC. Surely it should be available to Ruby via Bridge Support.

However, a system-wide keyboard listener would be called very often. I would suggest to code in C or Obj-C instead, which would be more efficient.

Yuji
that's a good answer! thanks
luca