我编写了这段代码来观察按下键盘按键的事件。问题似乎是,当运行此脚本时,特定程序将导致此程序崩溃,并输出以下错误消息:
TypeError: KeyboardSwitch() missing 8 required positional arguments: 'msg', 'vk_
code', 'scan_code', 'ascii', 'flags', 'time', 'hwnd', and 'win_name'
一些经常崩溃的程序有:Skype,Sublime Text 2。
经过几次尝试调试后,问题似乎出现在最后一行,但我似乎无法缩小范围。我也不明白编译器返回的KeyboardSwitch()的含义...
我还发现该程序会交替返回此错误消息
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch
return func(event)
File "observe.py", line 6, in OnKeyboardEvent
print ('MessageName:',event.MessageName)
TypeError: an integer is required (got type NoneType)
是什么原因导致的?如何修复它,特别是因为只有每按两个键中的1个才会出现?
import pyHook, pythoncom
def OnKeyboardEvent(event):
# Source: http://code.activestate.com/recipes/553270-using-pyhook-to-block-windows-keys/
print ('MessageName:',event.MessageName)
print ('Message:',event.Message)
print ('Time:',event.Time)
print ('Window:',event.Window)
print ('WindowName:',event.WindowName)
print ('Ascii:', event.Ascii, chr(event.Ascii))
print ('Key:', event.Key)
print ('KeyID:', event.KeyID)
print ('ScanCode:', event.ScanCode)
print ('Extended:', event.Extended)
print ('Injected:', event.Injected)
print ('Alt', event.Alt)
print ('Transition', event.Transition)
print ('---')
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
P.S. 作为一个初学者,我对 pythoncom 的功能不是很熟悉,而在线的定义似乎相当模糊。若能解释一下 pythoncom 和 PumpMessages 的功能,将不胜感激。
谢谢