Python Alt Hooking

五迷三道 提交于 2020-01-04 15:33:48

问题


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

回答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.



来源:https://stackoverflow.com/questions/13494234/python-alt-hooking

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