Context: I am developing a mouse and keyboard listener with PyHook.
Experience in python: Beginner
Python interpreter: 3.7
Problem: When I move the mouse over the header Google Chrome browser and the current website is Spotify, I am getting the following error:
TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags', 'time', 'hwnd', and 'window_name'.
What I found: I found the following related post in this community:
pythoncom crashes on KeyDown when used hooked to certain applications
In that post, the solution indicates that there is a bug with Pyhook when it tries to interpret the window name as ASCII. Based on this answer, the post suggests changing the source C code of Pyhook. Also, I found the following post, which explains how to modify in general a library in Python:
How to rebuild a python module/libary
Question: Is there another workaround to overcome this issue without modifying the source code?. In case the only option is by changing the source code, could someone give me more details about:
- How to find the source code of this library
- How to implement the suggested changes in that post with more explanation to someone who has no many experiences with Python.
Stacktrace:
File "C:\....\Local\Programs\Python\Python37\lib\site-packages\pyHook\HookManager.py", line 322, in MouseSwitch
event = MouseEvent(msg, x, y, data, flags, time, hwnd, window_name)
SystemError: <class 'pyHook.HookManager.MouseEvent'> returned a result with an error set
TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags', 'time', 'hwnd', and 'window_name'
Thank you for your help. Please forgive me if I explained something incorrectly.
Piece of code:
import pyHook
import pythoncom
import traceback
import threading
lock = threading.Lock()
try:
def create_element(event):
global t
lock.adquire()
print('MessageName:', event.MessageName)
lock.release()
def onMouseEvent(event):
global t
t = threading.Thread(target=create_element, args=(event))
t.start()
def onKeyboardEvent(event):
global t
t = threading.Thread(target=create_element, args=(event))
t.start()
hm = pyHook.HookManager()
hm.KeyDown = onKeyboardEvent
hm.MouseAll = onMouseEvent
hm.HookKeyboard()
hm.HookMouse()
pythoncom.PumpMessages()
except:
hm.UnhookMouse()
hm.UnhookKeyboard()
hm=None
traceback.print_exc()