Initial Project: Troubles with Non-functional Auto-Clicker Code

To avoid repetition, you should extract the logic of the button click handler and create a separate function. Instead of emulating the click event, you can then run this function using a timer. Additionally, it is recommended to bind the handler using addEventListener rather than onclick attribute, as it provides a better way of handling events.
Question:
I am attempting to create a basic auto clicker using python3 pyautogui. It allows users to select the coordinates for the clicking location. However, when I run the code, I encounter error messages despite there being no apparent issues. I have only just started coding yesterday.

Question:

I’m encountering difficulties understanding why my code isn’t working when I attempt to create an autoclicker using pynput as my initial project. The purpose of the code is to initiate or terminate clicking whenever I press “ctrl+alt+i” and perform a single click every second. Below is my current code. Although I don’t fully comprehend why it doesn’t function, I have managed to make the “click_thread.running” change from true to
false, python
when listening to my keyboard, which enables the clicking to work. However, it only operates when I set “self.running” to true in the “ClickMouse” class. Although the output of printing “click_thread.running” appears to switch from true to false, I’m unsure why the clicking does not commence. I suspect this may be due to it being a subclass of “threading.Thread” or a potential error in my class implementation. Regardless, I have been working on this for a few days and I’m currently stuck, seeking assistance to resolve the issue. Any help would be greatly appreciated!

import time
import threading
from pynput.mouse import Button, Controller
from pynput import keyboard
delay = 1
button = Button.left
class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super().__init__() 
        self.delay = delay
        self.button = button                
        self.running = False             
    def run(self):
        while self.running == True:
            mouse.click(self.button)
            time.sleep(self.delay)
    def start_clicking(self):
        self.running = True
    def stop_clicking(self):
        self.running = False
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
def on_activate_i():
    print('++i pressed')
    if click_thread.running == False:   
        click_thread.start_clicking()
    else:
        click_thread.stop_clicking()
    print(click_thread.running)
with keyboard.GlobalHotKeys({'++i': on_activate_i,}) as h:
    h.join()



Solution:


When you make a call to

click_thread.start()

, the handler associated with

start

will invoke your

run

function on a separate thread. Consequently,

self.running

will be

False

. Consequently, your while loop will terminate immediately, bringing an end to the thread.

Make

running=True

the default and delay creating the thread until

on_activate_i

.

Where are you clicking? At random?

Frequently Asked Questions