Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pynput - program lags upon start

So I've been trying to make a simple program that, upon clicking the right mouse button, makes my mouse click the left button 3 times in 0.5 second intervals. However, when I start the program and do right click, the program does what it's told to do but also starts lagging horrendously for about 25 seconds. After it's done lagging and I try to close the program, it freezes, forcing me to close it via task manager.

The code is as follows:

import time
from pynput.mouse import Button, Controller, Listener

mouse = Controller()

def on_click(x, y, button, pressed):
    if button == Button.right:
        num = 3
        while num > 0:
            time.sleep(0.5)
            mouse.click(Button.left)
            num -= 1

with Listener(on_click=on_click) as listener:
    listener.join()

Any help is greatly appreciated.

like image 459
Autonomous Variable Avatar asked Oct 29 '25 04:10

Autonomous Variable


1 Answers

After some time debugging and digging though issues, it seems that pynput.mouse.Listener has a few problems hanging/lagging on Windows machines when moving the mouse.

On a Linux machine, things should work fine out of the box with no hanging or lagging.

like image 197
Jebby Avatar answered Oct 30 '25 21:10

Jebby