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:
- Using PyAutoGui
- Using Pynput
To install pyautogui, type the following command:
pip install pyautogui
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
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()
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.
i am getting the error message “click_thread.start()
NameError: name ‘click_thread’ is not defined” on Thonny on a raspberry pi running raspian.
Hi, We’ve fixed the code as of now. Please check it and let us know.
AttributeError: type object ‘Button’ has no attribute ‘left’
When starting to auto-click
No, It’s working correctly. Make sure you are running it with python3. Press ‘s’ to start-stop auto clicking.
Question: Can you tell me how i can download this library in google collab?
No, you can’t run it on Google colab since there is no active display.
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?
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):
for x in range(0,image.size):
pixel = image.getpixel( (x,y) )
if pixel == YOUR_PIXEL_VALUE:
I was wondering how to increase click speed with a super simple gui bot. like.
for i in range (250):
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?
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
Would there be there a possibility to start the Auto Clicker with the F1 key?
Yes, use start_stop_key = KeyCode(char=’f1′) in the code. Then you can start it with f1.