pynput
pynput copied to clipboard
Making mouse and keyboard listeners after stopping both takes several seconds
I'm trying to make a Macro recording program with Pynput where listeners need to be switched at variable times. Therefore I'm trying to start mouse and keyboard listeners and then later when a trigger occurs, stop them, and then later start them up (by making new listeners that have the same callbacks of course).
However, when I stop both listeners by calling mouse.Listener.stop(mouseListener)
and keyboard.Listener.stop(keyboardListener)
, and the try to start new listeners up again, it takes several seconds just to execute mouse.Listener(...)
and keyboard.Listener(...)
from pynput import mouse, keyboard
import threading
def start():
print('start')
mListener = mouse.Listener(on_move=lambda x, y: print('on_move'))
kListener = keyboard.Listener(on_press=lambda key: print('on_press'))
mListener.start()
kListener.start()
return (mListener, kListener)
def stop(mListener, kListener):
mouse.Listener.stop(mListener)
keyboard.Listener.stop(kListener)
print('stop')
(mListener, kListener) = start()
threading.Timer(2, lambda: stop(mListener, kListener)).start()
threading.Timer(5, lambda: start()).start()
while True:
pass
After the first timer stops the listeners and the second timer triggers, several seconds pass by between 'start' being printed and the start() function finishes.