Python for Autohotkey style key-combination sniffing, automation?

为君一笑 提交于 2019-12-03 03:21:20

问题


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.

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.

Basically autohotkey.

Why not just use autohotkey? I actually have quite a few autohotkey macros, but I'd prefer to use a saner language.

My question is: is there a good way to have a background python process detect key combinations?

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()

回答1:


You may want to look at AutoIt. It does everything that AutoHotKey can do, but the language syntax doesn't make you want to pull your hair out. Additonally, it has COM bindings so you can use most of it's abilities easily in python if you so desired. I've posted about how to do it here before.




回答2:


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()


来源:https://stackoverflow.com/questions/294285/python-for-autohotkey-style-key-combination-sniffing-automation

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