From 152f53e0141d5d24b2d4d937f0418c3b96b27726 Mon Sep 17 00:00:00 2001 From: Tithi Joshi Date: Mon, 16 Feb 2026 11:26:16 +0530 Subject: [PATCH] refactor: improve structure and safety of auto clicker script --- Auto-Clicker/auto_clicker.py | 57 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/Auto-Clicker/auto_clicker.py b/Auto-Clicker/auto_clicker.py index c0c9d23..76f698b 100644 --- a/Auto-Clicker/auto_clicker.py +++ b/Auto-Clicker/auto_clicker.py @@ -2,33 +2,48 @@ import keyboard import time -clicking = False +def run_auto_clicker(delay: float = 0.01) -> None: + """ + Runs an auto clicker that can be controlled with keyboard hotkeys. -def start_clicking(): - global clicking - clicking = True - print("Auto clicker started") + Controls: + - Press 'S' to start clicking + - Press 'E' to stop clicking + - Press 'Q' to quit + """ + clicking = False + def start_clicking(): + nonlocal clicking + clicking = True + print("✅ Auto clicker started") -def stop_clicking(): - global clicking - clicking = False - print("Auto clicker stopped") + def stop_clicking(): + nonlocal clicking + clicking = False + print("⏹ Auto clicker stopped") + + keyboard.add_hotkey("s", start_clicking) + keyboard.add_hotkey("e", stop_clicking) + + print("Press 'S' to start clicking") + print("Press 'E' to stop clicking") + print("Press 'Q' to quit") + try: + while True: + if clicking: + pyautogui.click() + time.sleep(delay) -keyboard.add_hotkey("s", start_clicking) -keyboard.add_hotkey("e", stop_clicking) + if keyboard.is_pressed("q"): + print("👋 Exiting program") + break -print("Press 'S' to start clicking") -print("Press 'E' to stop clicking") -print("Press 'Q' to quit") + except KeyboardInterrupt: + print("\nProgram interrupted by user.") -while True: - if clicking: - pyautogui.click() - time.sleep(0.001) - if keyboard.is_pressed("q"): - print("Exiting program") - break +if __name__ == "__main__": + run_auto_clicker()