用于 Autohotkey 风格的组合键嗅探、自动化的 Python? [英] Python for Autohotkey style key-combination sniffing, automation?

查看:36
本文介绍了用于 Autohotkey 风格的组合键嗅探、自动化的 Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动化几个任务(例如,模拟 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆