'Trying to make an autoclicker using python

So Im trying to make an autoclicker using python(Im still new to python), and the auto clicker itself works. And now I am trying to make it possible to add delay between clicks, and remove delay between the clicks, but it doesnt work for some reason. Does anyone know why?

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

global delay
delay = 0.005
button = Button.left
start_stop_key = KeyCode(char="n")
exit_key = KeyCode(char="e")

up_key = pynput.keyboard.Key[0]
down_key = pynput.keyboard.Key[0]

class ClickMouse(threading.Thread) :
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self): 
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)

mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()

def on_press(key):
    delay = 0.005
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()

    elif key == exit_key: 
        click_thread.exit()
        listener.stop()

    elif key == down_key:
        delay += 0.005

    elif key == up_key:
        delay -= 0.005

with Listener(on_press=on_press) as listener:
    listener.join()


Solution 1:[1]

I don't know if this will be possible because the clicking thread is being run separately from the keyboard listener thread. Even though it can recognize you are hitting the arrow key, it cannot pass the delay variable into the other thread easily. This would be much easier if you wrote it so that it is all inside of one thread and you could listen from inside of the thread to change the delay.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EdGe Sloth