How can I avoid TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'time', 'hwnd', and 'window_name'

不想你离开。 提交于 2019-12-19 03:42:28

问题


Trying to hook into mouse events but in my early tests the program stops responding after about 30 seconds[EDIT: See bottom of post] and gives this error

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

Any help would be appreciated.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/27363268/how-can-i-avoid-typeerror-mouseswitch-missing-8-required-positional-arguments

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