如何避免TypeError:MouseSwitch()缺少8个必需的位置参数:'msg','x','y','data','time','hwnd'和'window_name' [英] How can I avoid TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'time', 'hwnd', and 'window_name'
问题描述
试图陷入鼠标事件,但在我的早期测试中,程序在30秒后停止响应,并给出此错误
Trying to hook into mouse events but in my early tests the program stops responding after about 30 seconds and gives this error
TypeError:MouseSwitch()缺少8个必需的位置参数:"msg","x","y",数据",时间","hwnd"和"window_name"
TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'time', 'hwnd', and 'window_name'
这是代码.它应该只打印所有事件信息,直到崩溃为止.
Here's the code. It's supposed to just print all the event info, which it does until it crashes.
import pythoncom
import pyHook
def OnMouseEvent(event):
print ('MessageName:',event.MessageName)
print ('Message:',event.Message)
print ('Time:',event.Time)
print ('Window:',event.Window)
print ('WindowName:',event.WindowName)
print ('Position:',event.Position)
print ('Wheel:',event.Wheel)
print ('Injected:',event.Injected)
print ('---')
return True
hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()
任何帮助将不胜感激.
更新! 经过进一步测试后,崩溃似乎仅发生在将鼠标悬停在某些窗口(例如Skype联系人列表)上时.如果我将鼠标悬停在google chrome窗口的标题上,也会收到相同的错误消息(但不会崩溃).
UPDATE! Having done some further testing, the crash only seems to happen when mousing over certain windows (such as the skype contact list). I also get the same error message (but with no crash) if I mouse over the header of a google chrome window.
推荐答案
当pyHook尝试将窗口名称解释为ascii时,我使用KeyboardSwitch
而不是MouseSwitch
进行了跟踪,并将其追溯到了UnicodeDecodeError
.在其窗口名称中包含Unicode字符的Skype上,此操作将失败.我已经在此处上发布了修复方法. >.但是我不得不重建pyHook.
I had this with KeyboardSwitch
instead of MouseSwitch
and traced it to a UnicodeDecodeError
when pyHook tries to interpret the window name as ascii. It fails on Skype which has unicode characters in its window name. I've posted how I fixed it here. But I had to rebuild pyHook.
这篇关于如何避免TypeError:MouseSwitch()缺少8个必需的位置参数:'msg','x','y','data','time','hwnd'和'window_name'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!