How to add On and Off buttons with pyautogui?

Viewed 382

I made a simple auto-clicker in python. Every three seconds the script clicks wherever your mouse is. How would I add "on and off" keys? I imagine it is a simple if/else statement but I don't know how to write it.

As of Wed Sep 15 12:10, I do not have an answer that works well.

import pyautogui
import time 

def Auto_Click():
    width, height = pyautogui.position()
    pyautogui.click(width, height)
    time.sleep(3)

while True:
    Auto_Click()
1 Answers

I'd suggest listening to specific key presses indefinitely to switch clicking on and off. And as there is an indefinite loop for the clicking as well, you will need multithreading (to perform clicking and listening for key presses simultaneously).

Notes

  • The auto clicker is switched off by default right on start (To avoid clicks at unwanted positions on screen right after running it). Press SHIFT to toggle it after pointing the mouse at wanted position.
  • Press ESC to exit the program.
  • I have used SHIFT and ESC keys for toggles so that the key presses won't show up in the next prompt unlike the character keys.
  • Use the below code if you really need to use pyautogui. Here is the solution using pynput for handling both mouse and keyboard. (My code is basically a modified version which uses keyboard module and pyautogui instead)
import time
import keyboard
import pyautogui
import threading

INTERVAL = 0.5          # Time interval between consecutive clicks
DELAY = 0.5             # Time delay between consecutive program cycles [after the clicks are turned off]
TOGGLE_KEY = 'shift'    # Key to toggle the clicking
EXIT_KEY = 'esc'        # Key to stop and exit from the program

class AutoClicker(threading.Thread):
    def __init__(self, interval, delay):
        super(AutoClicker, self).__init__()
        self.interval = interval
        self.delay = delay
        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 toggle_clicking(self):
        if self.running:
            self.stop_clicking()
        else:
            self.start_clicking()

    def click(self):
        width, height = pyautogui.position()
        pyautogui.click(width, height)

    # This function is invoked when the thread starts.
    def run(self):
        while self.program_running:
            while self.running:
                self.click()
                time.sleep(self.interval)
            time.sleep(self.delay)

if __name__ == '__main__':
    # Run indefinite loop of clicking on seperate thread
    auto_clicker_thread = AutoClicker(INTERVAL, DELAY)
    auto_clicker_thread.start()  # Invokes run() function of the thread

    # So that we can listen for key presses on the main thread
    keyboard.add_hotkey(TOGGLE_KEY, lambda: auto_clicker_thread.toggle_clicking())
    keyboard.add_hotkey(EXIT_KEY, lambda: auto_clicker_thread.exit())
Related