Python Alt挂钩 [英] Python Alt Hooking

查看:100
本文介绍了Python Alt挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题时,我正在编写此类型记录程序- Alt 键没有Ascii编号,因此无法以常规方式对其进行挂钩. 这是没有 Alt 钩子尝试的源代码,问题是-如何钩住 Alt ? 我知道有一个名为"Alt"的Class变量和一个名为"IsAlt"的内置函数,但我不知道如何使用它们.

I was writing this type-recording program when I encountered a problem - Alt key doesn't have an Ascii number so I can't hook it in the regular way. This is my source code without the Alt hooking try, the question is - how do I hook Alt? I know that there is Class variable named "Alt" and built-in function named "IsAlt" but I didn't get how to use them.

import pythoncom,pyHook

log = ""
logpath = "log.txt"

openfile = open(logpath,"w")
openfile.write("")

def OnKeyboardEvent(event):
    try:
        global log
        if event.Ascii == 8:
            log = "[BS]"
        elif event.Ascii == 9:
            log = "[TAB]"
        elif event.Ascii == 13:
            log = "[NL]"
        elif event.Ascii == 27:
            log = "[ESC]"
        elif event.Ascii == 15:
            openfile.close()
            exit()
        else:
            log = chr(event.Ascii)
        openfile.write(log)
    except:
        pass

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

推荐答案

我喜欢它!不要使用"event.Ascii"来映射键,而要使用"event.KeyID"! 请注意,对于诸如"AltGr"之类的键,您有2个映射键ID:1为按下的键,其他为释放的键.祝你有美好的一天.

I foud it!! Instead of using "event.Ascii" to map keys, use "event.KeyID"!! Note that for keys like "AltGr" you have 2 mapping key IDs: 1 for key pressed and other for key released. Have a good day.

这篇关于Python Alt挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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