How to Make Auto Clicker in Python | Auto Clicker Script

Hello coders!! In this article, we will understand the concept of auto clicker in python. We will first understand its meaning and its implementation in python. So without wasting any time, let’s get straight into the topic.

What is an auto clicker?

Auto clicker is a script available in python that facilitates the user to repeatedly clicking their mouse within small delay intervals. It is controlled by user-defined keys and works on every environment – Windows, Mac, and Linux. To achieve this, we will use a module called PyAutoGUI in Python. This will allow us to control the mouse and monitor the keyboard at the same time.

Ways to Create an Auto Clicker in Python:

  1. Using PyAutoGui
  2. Using Pynput

PyAutoGUI Installation:

To install pyautogui, type the following command:

pip install pyautogui
PyAutoGUI Installation
PyAutoGUI Intslaltion

Key features of PyAutoGUI:

  • Sends Keystrokes to applications
  • Moves the mouse and clicks in the windows of other applications
  • Locate an application’s window
  • Display of message boxes for user interaction

Keyboard and Mouse Controls using PyAutoGUI:

PyAutoGUI uses the (x,y) coordinate with the origin (0,0) at the screen’s top-left corner. The x coordinates increase as we go to the right, whereas the ‘y’ coordinates increase as we go down. Currently, PyAutoGUI works only on the primary monitor. It isn’t reliable for the screen of a second monitor. All the presses on the keyboard done by PyAutoGUI are delivered to the window that currently has focus.

Making auto clicker in Python using PyAutoGUI:

import pyautogui
import time

def click(): 
    time.sleep(0.1)     
    pyautogui.click()

def main():
    for i in range(20): 
        click()

main()

Making Auto Clicker in Python using Pynput:

Pynput is a module available in python used to control mouse movements. It can be used to make an auto clicker in python.

Installation of Pynput:

To install pynput, type the following command:

pip install pynput
Installation of Pynput to make auto clicker in python
Installing Pynput
import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode


delay = 0.001
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')


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)


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


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()

Explanation:

Importing required modules:

  • Button and controller: to control the mouse.
  • Listener and KeyCode: to watch for keyboard events to start and stop the python auto clicker.

Declaring important variables:

  • button: a button that will be clicked.
  • begin_end: the key that we will use to start and stop the auto clicker.
  • exit_key: to close and reset the program and reset everything.

Creating a class to extend threading:

The thread created will allow us to control the mouse clicks. There are two parameters – delay and button. There are also two flags about whether the program is running or not.

Creating methods to handle the thread externally:

  • begin(): starts the thread
  • end(): stops the thread
  • exit(): exits the program and resets

Creating a method that will run when the thread starts:

This method will run once the thread starts. We will keep iterating through the loop till the value of run_prgm is True. The loop inside the first loop is iterated till the value of a run is True. Once we are inside both the loops, we click the set button.

Creating an instance for mouse controller:

The Mouse_click thread that we created will start when the user gets into the loop in the run method.

Creating a method to setup keyboard listener:

If the key pressed is the begin_end, it will stop clicking given that the flag is set to true. Otherwise, it will start it. If the key pressed is the exit_key, the exit method is called in the thread and stop the listener.

More >> Interesting Python Frameworks You Should Know

Conclusion: Python Auto Clicker

These are two different ways in which one can create an auto clicker in python. It can be further modified as per one’s requirement.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

0 0 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
carter
carter
4 months ago

i am getting the error message “click_thread.start()
NameError: name ‘click_thread’ is not defined” on Thonny on a raspberry pi running raspian.

Python Pool
Admin
4 months ago
Reply to  carter

Hi, We’ve fixed the code as of now. Please check it and let us know.

lowlife
lowlife
1 month ago

AttributeError: type object ‘Button’ has no attribute ‘left’

When starting to auto-click

Pratik Kinage
Admin
Pratik Kinage
1 month ago
Reply to  lowlife

No, It’s working correctly. Make sure you are running it with python3. Press ‘s’ to start-stop auto clicking.