Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making an autoclicker in python?

I have designed this simple autoclicker in python using the pynput library

import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode

TOGGLE_KEY = KeyCode(char="t")
activated = False
mouse = Controller()

def on_press(key):
    global activated
    if key == TOGGLE_KEY:
        activated = not activated

        while activated:
            mouse.click(Button.left, 1)
            time.sleep(0.01)


listner = Listener(on_press=on_press)
listner.start()

input()

This code activates a listner and when the user presses any key it calls the on_press function which checks if the button is equal to ‘t’ then it inverts the property of activated and starts the while loop

I tried the above code and it worked but when I pressed ‘t’ again the autoclicker did not switch off

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe you are stuck in your while activated: loop. Pushing T again does not make the function run again unless the first function has stopped. A simple solution would be to put the click event in its own thread.

import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode

TOGGLE_KEY = KeyCode(char="t")
activated = False
mouse = Controller()

def on_press(key):
    global activated
    if key == TOGGLE_KEY:
        activated = not activated

def doClick():
    global activated
    while True:
        if activated:
            mouse.click(Button.left, 1)
            time.sleep(0.01)

threading.Thread(target = doClick).start()

listner = Listener(on_press=on_press)
listner.start()

input()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading