pythonpython-3.xpynput

How do I prevent lag when using mouse.Listener() with pynput?


When using the mouse.Listener() function in pynput, my cursor lags and becomes almost unusable. Is there any way to prevent this lag, or is it simply a consequence of using pynput?

from PIL import ImageGrab
import cv2
import numpy as np
from pynput import mouse

side_button_held = False

def on_click(x, y, button, pressed):
    global side_button_held
    if button == mouse.Button.x2 and pressed:
        side_button_held = True
        print("Side button is held down")
    elif button == mouse.Button.x2:
        side_button_held = False
        print("Side button is released")
        
listener = mouse.Listener(on_click=on_click)
listener.start()

while True:
    if side_button_held:
        screenshot = ImageGrab.grab(bbox=None)
        img = np.array(screenshot)
        rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        cv2.imshow("Screen", rgb_img)
        cv2.waitKey(1)

I tried adding a delay each time the loop runs by using the time.sleep() function in order to reduce the load on my computer, but that didn't seem to fix the issue.


Solution

  • I was able to fix this by using win32api instead of pynput.