Hi there, developers!! In this tutorial, we will look at the auto clicker in Python. We will first learn what it means and how to implement it in Python. So, without further ado, let’s get right to the point.
Auto clicker is a Python software that allows the user to continually click their mouse at short intervals. It is controlled by user-defined keys and works in all environments – Windows, Mac, and Linux. In Python, we will utilize a package named PyAutoGUI to do this. This will allow us to operate the mouse and monitor the keyboard at the same time.
Method 1: Using PyAutoGui
PyAutoGUI employs the (x,y) coordinate with the origin (0,0) at the top-left corner of the screen. The x coordinates grow as we move to the right, but the y coordinates decrease.
PyAutoGUI currently only works on the primary display. It is untrustworthy for a second monitor’s screen. All keyboard pushes performed by PyAutoGUI are transmitted to the window with the current focus.
import pyautogui import time def click(): time.sleep(0.1) pyautogui.click() for i in range(20): click()
Method 2: Using Pynput
Let’s try using the Pynput module to implement an autoclicker in Python.
Importing required modules
import time import threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode
There are multiple modules imported in the program including importing the button and controller in order to control the mouse actions and also the listener and keycodes in order to keep track of the keyboard events to handle the start and stop of the auto clicker actions.
Declaring important variables
delay = 0.001 button = Button.left start_stop_key = KeyCode(char='s') exit_key = KeyCode(char='e')
The next step is to declare some important variables including the following:
- Button variable which is set to the mouse button that needs clicking.
- Begin_End variable which is set to the key which starts and stops the autoclicker.
- Exit_Key variable to close the autoclicker.
Creating a class to extend threading
class ClickMouse(threading.Thread): def __init__(self, delay, button): super(ClickMouse, self).__init__() self.delay = delay self.button = button self.running = False self.program_run = True def start_clicking(self): self.running = True def stop_clicking(self): self.running = False def exit(self): self.stop_clicking() self.program_run = False def run(self): while self.program_run: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)
We’ll be able to manage the mouse clicks thanks to the thread we’ve constructed. There are two options: delay and button. There are additionally two indicators indicating whether or not the program is executing.
Creating methods to handle the thread externally
- start_clicking(): starts the thread
- stop_clicking (): stops the thread
- exit(): exits the program and resets
Creating a method that will run when the thread starts
When the thread starts, this method will be called. We shall iterate through the loop until the result of run_prgm equals True. The loop within the loop iterates until the value of a run is True. We press the set button once we’re within both loops.
Creating an instance for mouse controller
mouse = Controller() thread = ClickMouse(delay, button) thread.start()
Creating a method to setup keyboard listener
def on_press(key): if key == start_stop_key: if thread.running: thread.stop_clicking() else: thread.start_clicking() elif key == exit_key: thread.exit() listener.stop() with Listener(on_press=on_press) as listener: listener.join()
If you hit the begin end key, it will cease clicking if the flag is set to true. Otherwise, it will begin. If the exit key is pushed, the thread’s exit method is invoked, and the listener is terminated.
These are two distinct approaches to developing an auto clicker in Python. It may be further customized based on the needs of the user.
Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:
- 2 Ways to Capture Screenshots Using Python
- 4 Ways to Perform Random Sampling in NumPy
- Tricks for Easier Debugging in Python
Thank you for taking your time out! Hope you learned something new!! 😄