- Autoclicker in Python – 2 simple and easy ways
- Method 1: Using PyAutoGui
- Code Implementation
- Method 2: Using Pynput
- Importing required modules
- Declaring important variables
- Creating a class to extend threading
- Creating methods to handle the thread externally
- Creating a method that will run when the thread starts
- Creating an instance for mouse controller
- Creating a method to setup keyboard listener
- Conclusion
- Python Auto Clicker
Autoclicker in Python – 2 simple and easy ways
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.
Code Implementation
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_Endvariable which is set to the key which starts and stops the autoclicker.
- Exit_Keyvariable 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.
Conclusion
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:
Thank you for taking your time out! Hope you learned something new!! 😄
Python Auto Clicker
This is a script that allows you to click your mouse repeatedly with a small delay. It works on Windows, Mac and Linux and can be controlled with user-defined keys.
This project uses the cross-platform module pynput to control the mouse and monitor the keyboard at the same time to create a simple auto clicker.
If you haven’t used or setup pip before, look at my tutorial on how to setup python’s pip to setup pip.
We will be using the pynput module to listen to mouse events. To install this module execute pip install pynput in cmd. Watch the output to make sure no errors have occurred; it will tell you when the module has been successfully installed.
To double-check that it was installed successfully, open up IDLE and execute the command import pynput ; no errors should occur.
.
First, we need to import time and threading. Then import Button and Controller from pynput.mouse so we can control the mouse and import Listener and KeyCode from pynput.keyboard so we can watch for keyboard events to start and stop the auto clicker.
import time import threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode
Next create four variables as shown below. ‘delay’ will be the delay between each button click. ‘button’ will be the button to click, this can be either ‘Button.left’, ‘Button.right’ or even ‘Button.middle’. ‘start_stop_key’ is the key you want to use to start and stop the auto clicker. I have set it to the key ‘s’ to make it nice and simple, you can use any key here. Finally, the ‘exit_key’ is the key to close the program set it like before, but make sure it is a different key.
delay = 0.001 button = Button.left start_stop_key = KeyCode(char='s') exit_key = KeyCode(char='e')
Now create a class that extends threading.Thread that will allow us to control the mouse clicks. Pass they delay and button to this and have two flags that determine whether it is running or if the whole program is stopping.
class ClickMouse(threading.Thread): def __init__(self, delay, button): super(ClickMouse, self).__init__() self.delay = delay self.button = button self.running = False self.program_running = True
Next, add the methods shown below to control the thread externally.
def start_clicking(self): self.running = True def stop_clicking(self): self.running = False def exit(self): self.stop_clicking() self.program_running = False
Now we need to create the method that is run when the thread starts. We need to keep looping while the program_running is true and then create another loop inside that checks if the running is set to true. If we are inside both loops, click the set button and then sleep for the set delay.
def run(self): while self.program_running: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)
Now we want to create an instance of the mouse controller, create a ClickMouse thread and start it to get into the loop in the run method.
mouse = Controller() click_thread = ClickMouse(delay, button) click_thread.start()
Now create a method called on_press that takes a key as an argument and setup the keyboard listener.
def on_press(key): pass with Listener(on_press=on_press) as listener: listener.join()
Now modify the on_press method. If the key pressed is the same as the start_stop_key, stop clicking if the running flag is set to true in the thread otherwise start it. If the key pressed is the exit key, call the exit method in the thread and stop the listener. The new method will look like this:
def on_press(key): if key == start_stop_key: if click_thread.running: click_thread.stop_clicking() else: click_thread.start_clicking() elif key == exit_key: click_thread.exit() listener.stop()
This script can be saved as a .pyw to run in the background. It can easily be still closed using the set exit key even when no dialog is shown.
To use this script set the variables at the top to what you want.
- delay : They delay between each mouse click (in seconds)
- button : The mouse button to click; Button.left | Button.middle | Button.right
- start_stop_key : They key to start and stop clicking. Make sure this is either from the Key class or set using a KeyCode as shown.
- exit_key : The key to stop the program. Make sure this is either from the Key class or set using a KeyCode as shown.
Then run the script and use the start/stop key when wanted. Press the set exit key to exit.
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_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 run(self): while self.program_running: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1) mouse = Controller() click_thread = ClickMouse(delay, button) click_thread.start() def on_press(key): if key == start_stop_key: if click_thread.running: click_thread.stop_clicking() else: click_thread.start_clicking() elif key == exit_key: click_thread.exit() listener.stop() with Listener(on_press=on_press) as listener: listener.join()
Common Issues and Questions
ModuleNotFoundError/ImportError: No module named ‘pynput’
Did you install pynput? This error will not occur if you installed it properly. If you have multiple versions of Python, make sure you are installing pynput on the same version as what you are running the script with.
Syntax errors are caused by you and there is nothing I can offer to fix it apart from telling you to read the error. They always say where the error is in the output using a ^. Generally, people that get this issue have incorrect indentation, brackets in the wrong place or something spelt wrong. You can read about SyntaxError on Python’s docs here.
‘python’ is not recognized as an internal or external command
Python hasn’t been installed or it hasn’t been installed properly. Go to /blog/post/how-to-setup-pythons-pip/ and follow the tutorial. Just before you enter the scripts folder into the path variable, remove the «\scripts\» part at the end. You will also want to add another path with «\scripts\» to have pip.
Edited 11/08/18: Added Python 2 support
Owner of PyTutorials and creator of auto-py-to-exe. I enjoy making quick tutorials for people new to particular topics in Python and tools that help fix small things.