适用于Autohotkey样式的Python组合键嗅探,自动化吗? [英] Python for Autohotkey style key-combination sniffing, automation?
问题描述
我想自动化几个任务(例如,为其他编辑者模拟Eclipse风格的 ctrl - shift - R 打开对话框).通常的模式是:用户将按某种组合键,我的程序将检测到它,并可能弹出一个对话框以获取用户输入,然后运行相应的命令,通常是通过运行可执行文件.
I want to automate several tasks (eg. simulate eclipse style ctrl-shift-R open dialog for other editors). The general pattern is: the user will press some key combination, my program will detect it and potentially pop up a dialog to get user input, and then run a corresponding command, typically by running an executable.
我的目标环境是Windows,尽管跨平台会很好.我的程序将启动一次,读取配置文件,然后在后台运行,直到被组合键或其他事件触发为止.
My target environment is windows, although cross-platform would be nice. My program would be started once, read a configuration file, and sit in the background till triggered by a key combination or other event.
基本上是自动热键.
为什么不只使用autohotkey?实际上,我有很多自动热键宏,但我更喜欢使用较简洁的语言.
Why not just use autohotkey? I actually have quite a few autohotkey macros, but I'd prefer to use a saner language.
我的问题是:是否有一个好的方法让后台python进程检测键组合?
My question is: is there a good way to have a background python process detect key combinations?
更新:使用pyHook和win32扩展找到了答案:
Update: found the answer using pyHook and the win32 extensions:
import pyHook
import pythoncom
def OnKeyboardEvent(event):
print event.Ascii
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
while True:
pythoncom.PumpMessages()
推荐答案
使用pyHook和win32扩展找到了答案:
Found the answer using pyHook and the win32 extensions:
import pyHook
import pythoncom
def OnKeyboardEvent(event):
print event.Ascii
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
while True:
pythoncom.PumpMessages()
这篇关于适用于Autohotkey样式的Python组合键嗅探,自动化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!