3

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.

Jebby
  • 1,845
  • 1
  • 12
  • 25
  • I have reason to believe that this lag is a byproduct of `time.sleep()` inside a pynput listened function, consider utilizing sched to remove the need for `time.sleep()`. – EarthenSky Apr 27 '21 at 09:16

2 Answers2

2

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.

Jebby
  • 1,845
  • 1
  • 12
  • 25
  • Fast forward to nearly 2021 and the issue still prevails. It lags almost every time it gets a click event, and I haven't been able to find an alternative as `pyautogui`, according to its author, doesn't listen for such events and `pyhook` doesn't work on `3.x`. Any other possibilities? – OakenDuck Dec 08 '20 at 18:59
  • @OakenDuck did you find a solution yet? – Troughy Jul 29 '23 at 12:37
1

You need to make use of the pressed variable. It seems to hold the value of whether the button is pressed or released.

Without this, the loop repeats another time when it is released as well.

This works as expected for me:

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

mouse = Controller()

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

with Listener(on_click=on_click) as listener:
    listener.join()
Jebby
  • 1,845
  • 1
  • 12
  • 25
  • Hmm, it certainly works better now, but the lag still persists. Now there's no infinite loop, but it still lags during the 3 clicks it makes. – Autonomous Variable Feb 17 '19 at 09:35
  • @AutonomousVariable I did not notice any lag when running this (or your original code for that matter). How exactly is it lagging? What is happening? How are you monitoring the lag? – Jebby Feb 17 '19 at 09:46
  • @AutonomousVariable Ah, yes, that does seem to be very bad lag. Have you tried running the script outside of IDLE from a normal command prompt like so? `python script.py` Does that have the same result? – Jebby Feb 17 '19 at 10:10
  • Yes, same horrendous lag. – Autonomous Variable Feb 17 '19 at 10:11
  • The funny thing is, that I made a very similar program before and that doesn't lag. This is the first time I have experienced this issue. – Autonomous Variable Feb 17 '19 at 10:13
  • @AutonomousVariable Hmm, I'm not quite sure. I have no clue why it would do this out of nowhere, and I may not be of much more help. When you click the mouse, are you clicking once or are you rapidly clicking? I do get sent into a loop if I rapidly click, but not just once or twice. – Jebby Feb 17 '19 at 10:26
  • @AutonomousVariable Other than that, the only other idea I have is when you wrote the program before, was it on the same machine? Same OS? If the answer to both are yes, I'm officially stumped. – Jebby Feb 17 '19 at 10:30
  • Just one click. I'm gonna post the script of my other program that doesn't lag just in case. https://pastebin.com/2K8wjyj5 – Autonomous Variable Feb 17 '19 at 10:30
  • And yes, everything is the same. I created the other program literally a day before this one. – Autonomous Variable Feb 17 '19 at 10:32
  • @AutonomousVariable Do you mind if we [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188539/pynput-mouse-lagging)? I don't want to clutter the comments more than neccesary if it can be helped. – Jebby Feb 17 '19 at 10:40
  • Sure, but it says I need at least 20 reputation to talk there. – Autonomous Variable Feb 17 '19 at 10:41
  • @AutonomousVariable Can you join now? – Jebby Feb 17 '19 at 10:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188540/discussion-between-autonomous-variable-and-jebby). – Autonomous Variable Feb 17 '19 at 10:45