This is the code:
from pynput import keyboard, mouse
from time import sleep
clicked = False
mouseC = mouse.Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.left and pressed:
if not clicked:
execute()
def execute():
global clicked
clicked = True
sleep(0.1)
mouseC.press(mouse.Button.left)
mouseC.release(mouse.Button.left)
clicked = False
with mouse.Listener(on_click = on_click) as lstnr:
lstnr.join()
So, i'am trying to make a macro in Python using Pynput that triggers another click when the left mouse button is clicked, but i'am having a problem with it. When the second click is generated by the script it is detected by the Pynput listener and that creates a infinite loop of click.
I already try to solve this problem by making a condition in the "on_click" listener that only triggers if the a variable "clicked" is set to False. When the listener triggers, it execute a function that set the "clicked" variable to True right before the second click is generated and after that it set the variable to False again, allowing you to make another click. But that doenst seams to work and somehow the clicking loop still happening.
I hope you guys understand my bad english.
I accidentally discovery how to make it work. So i just add a count variable that increase by 1 each time a click is detected and create a condition that the second click will only hapen if the count hites 2. I don't know exaclly how this works but it works so i'm happy with it.
The code:
from pynput import mouse
from time import sleep
mouseC = mouse.Controller()
count = 0
def on_click(x, y, button, pressed):
global count
if button == mouse.Button.left and pressed:
count = count + 1
if count == 2:
count = 0
sleep(0.1)
mouseC.click(mouse.Button.left)
with mouse.Listener(on_click=on_click) as listener:
listener.join()