问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/2176836/make-applescript-program-that-listens-systemwide-for-shortcuts