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, let’s get straight into the topic without wasting any time.

What is an auto clicker?

Auto clicker is a script available in Python that facilitates the user to click their mouse within small delay intervals repeatedly. 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 that is 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 stops 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!

Subscribe
Notify of
guest
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
carter
carter
3 years 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
3 years ago
Reply to  carter

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

lowlife
lowlife
3 years ago

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

When starting to auto-click

Pratik Kinage
Admin
3 years ago
Reply to  lowlife

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

CoderMe
CoderMe
3 years ago

Question: Can you tell me how i can download this library in google collab?

Python Pool
Admin
3 years ago
Reply to  CoderMe

No, you can’t run it on Google colab since there is no active display.

fizzy
fizzy
2 years ago

I know im late but is it possible to create an autoclicker that can press a moving button by detecting the pixel color? if so, how?

Pratik Kinage
Admin
2 years ago
Reply to  fizzy

Yes, but I dont recommend doing that. It would slow down your processes by significant time.
For doing this, you need to loop through your screen and match the color.

from PIL import ImageGrab

image = ImageGrab.grab()
for y in range(0,image.size[1]):
for x in range(0,image.size[0]):
pixel = image.getpixel( (x,y) )
if pixel == YOUR_PIXEL_VALUE:

Regards,
Pratik

TJ w
TJ w
2 years ago

I was wondering how to increase click speed with a super simple gui bot. like.

import pyautogui

for i in range (250):
 pyautogui.click(430, 468)

Currently CPS is around 10. I was looking to increase this to maximum. I’ve read some blogs suggest using pyautogui.pause = 0, but I’m not having any change with this. Any suggestions?

Pratik Kinage
Admin
2 years ago
Reply to  TJ w

Yes, you can use pause to control the click per second. Set pyautogui.PAUSE to 0 to increase the CPS to maximum.

Driver code will look like this –

pyautogui.PAUSE = 0
Condition:
pyautogui.click(430, 468)

Regards,
Pratik

Dan
Dan
2 years ago

Would there be there a possibility to start the Auto Clicker with the F1 key?

Pratik Kinage
Admin
2 years ago
Reply to  Dan

Yes, use start_stop_key = KeyCode(char=’f1′) in the code. Then you can start it with f1.

Regards,
Pratik