2

I create a new thread and run the hook, but when I try to do something with the mouse or keyboard on the window, it does not respond and crashes, I've been trying for a long time, but I couldn't find a solution.

import threading

import pyWinhook as pyHook
import pythoncom
import tkinter as tk

def OnKeyboardEvent(event):
    return True

def OnMouseEvent(event):
    print("+")
    return True

def hook():
    hm = pyHook.HookManager()

    hm.MouseAllButtons = OnMouseEvent
    hm.KeyAll = OnKeyboardEvent

    hm.HookMouse()
    hm.HookKeyboard()
    pythoncom.PumpMessages()


root = tk.Tk()
threading.Thread(target=hook).start()
root.mainloop()
  • Are you using the mouse and the keyboard in the Python IDLE shell? If so, then it's normal. – SAL Jul 22 '22 at 11:12
  • this problem happens only when i hover over tkinter menu –  Jul 22 '22 at 11:26
  • This also happens to me. You could try to use another library similar to `pyWinhook`. For example, you can try `pynput`. – SAL Jul 22 '22 at 16:45

0 Answers0