2

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

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
sergioMoreno
  • 189
  • 1
  • 15
  • 1
    Please provide [mcve] and full stacktrace. – Alastair McCormack Dec 16 '19 at 13:57
  • Hi @AlastairMcComark. I added a piece of code that I am doing. What I found that Pyhook crashes with some particular softwares like Skype, Google Chrome and so on. With microsoft excel my code works well – sergioMoreno Dec 16 '19 at 14:10
  • Can you provide the stacktrace? – Alastair McCormack Dec 16 '19 at 18:35
  • Hi @Alastair McComack. I imported the stacktrace library in my project because I do not know how to retrieve what you need. The message that I got with this is the same: TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags', 'time', 'hwnd', and 'window_name'. . Also I added more code. Is it enough for you?. Please forgive me but I do not have experience in how to report an error correctly – sergioMoreno Dec 18 '19 at 15:37
  • Hi @AlastairMcCormack, did you find a solution regarding this issue that I have? – sergioMoreno Jan 20 '20 at 15:39
  • Please provide the stack trace in your question (A stack trace is the full output of the error. See https://realpython.com/python-traceback/) – Alastair McCormack Jan 20 '20 at 15:46
  • Hi, @AlastairMcCormack. I did not write to you for a long time because I was reading the link attached in your previous comment in order to understand how to retrieve the stack trace. However, I followed it and what I got was only the image that added today to my post and there you will see that there is no more detail in order to trace the the error that I got. Just for your information I am using Pycharm and there is an option call Analyze Staktrace but it shows me the same as you in the image above. Hopes you can help me because nobody has helped me so far. Have a nice day. – sergioMoreno Feb 17 '20 at 11:10
  • We need the trace (the bit that details how you go to the error). In your `except` statement, use `traceback.print_exc()` and please don't use screenshots. – Alastair McCormack Feb 17 '20 at 11:19
  • Hi, @AlastairMcCormack I added what you suggested in the code and also I wrote the traceback that I got. I hope this is what you require. Tks again for your patience and your help. – sergioMoreno Feb 19 '20 at 14:41

0 Answers0