make AppleScript program that listens systemwide for shortcuts

喜夏-厌秋 提交于 2019-12-24 05:05:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!