Not clicking when user presses mouse button

25 Views Asked by At

I want that when the user presses a mouse button, that button clicks until the user releases it. To do this, I check if the button is pressed and if so, I click it, but it doesn't work. I think that problem is connected with while. What am I doing wrong?

import threading
from pynput import mouse
from pynput.mouse import Button, Controller

clickingMouse = Controller()
rightTrue = False
leftTrue = False

def on_click(x, y, button, pressed):
    global leftTrue, rightTrue

    if button == Button.left:
        leftTrue = pressed
        print(leftTrue)
    elif button == Button.right:
        rightTrue = pressed
        print(rightTrue)

def Check():
    while leftTrue:
        print("clicking left!")
        clickingMouse.click(Button.left, 1)
    while rightTrue:
        print("clicking right!")
        clickingMouse.click(Button.right, 1)


check = threading.Thread(target=Check)
check.start()

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

I tried this, but it was half working.

def Check():
    while True:
        if leftTrue:
            print("clicking left!")
            clickingMouse.click(Button.left, 1)

0

There are 0 best solutions below