How to make a Python auto clicker?
Last Updated :
19 Jan, 2021
In this article, we will see how to create an auto-clicker using Python. The code will take input from the keyboard when the user clicks on the start key and terminates auto clicker when the user clicks on exit key, the auto clicker starts clicking wherever the pointer is placed on the screen. We are going to use the pynput module here.
What is Auto Clicker?
Auto-Clicker is a script where you can auto control mouse and keyboard as many numbers of times as you want. It is controlled using user-defined keys. It works on various platforms like Windows, Mac and Linux. Auto clicker is present in pywin32 module.
Approach:
In this project, we will use a cross-platform module pynput to control the mouse and monitor the keyboard at the same time to create simple auto-clicker. To check for mouse events we will install pynput module (used to control the mouse) for this execute, pip install pynput in cmd.
Note: If you’re stuck on how to set up python-pip package on your system then click here
Installation of pynput module
Verify whether the pynput module has been successfully installed into your working environment for this, open IDLE on the system that is cmd or Python Shell. Execute the command import pynput, after executing this the output should give zero errors which means your module is successfully installed.
Verifying module installation
Implementation:
Let’s now proceed with the code that is required to build an Auto-clicker using Python. Follow the below steps to create an auto-clicker:
Step 1: Import time and threading then import Button and Controller from pynput.mouse module. Import Listener and KeyCode from pynput.keyboard.
Python3
import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
|
Step 2: Create four variables as mentioned below,
- delay: Delay between each click (in seconds)
- button: Button is used to click in whatever direction you want to. Button.left | Button.middle | Button.right
- start_stop_key: The key used for start and stop of the click while you run the program for executing the auto clicker. It should be from a key class or set using KeyCode.
- exit_key: The key used to terminate the auto clicker that is being executed. This should be from the key class or set using KeyCode.
Python3
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char = 'a' )
stop_key = KeyCode(char = 'b' )
|
Step 3: Create a class extending threading.Thread. Pass delay and button to the class that have two flags to check if the program is executed or not.
Python3
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
|
Step 4: Add methods to control the threads externally.
Python3
def start_clicking( self ):
self .running = True
def stop_clicking( self ):
self .running = False
def exit( self ):
self .stop_clicking()
self .program_running = False
|
Step 5: A method is created when the thread starts, the program_running runs on loop until the value comes out to be true and also create another loop inside the existing loop where it checks if running is set to true or not. In case, we are inside both loops, it will click on the set button and sleep for the set delay.
Python3
def run( self ):
while self .program_running:
while self .running:
mouse.click( self .button)
time.sleep( self .delay)
time.sleep( 0.1 )
|
Step 6: Creating an instance for the mouse controller, then create ClickMouse thread. Start the instance to move into the loop inside the run method.
Python3
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
|
Step 7: Create a method on_press which takes a key as an argument and sets up a keyboard listener. The start_stop_key matches with a start key (a) when it is executed. Then the click is to be terminated when running flag is set to True in the thread. Exit method is called in the method if the exit key (b) is executed and stop the listener.
Python3
def on_press(key):
if key = = start_stop_key:
if click_thread.running:
click_thread.stop_clicking()
else :
click_thread.start_clicking()
elif key = = stop_key:
click_thread.exit()
listener.stop()
with Listener(on_press = on_press) as listener:
listener.join()
|
After the code is run we can see in the output as shown below, it shows the number of clicks the auto-clicker has made after the code is implemented. It is compatible with Windows, Mac and Linux. Auto-Clicker is helpful software for the systems as it let’s save a reasonable amount of time that is spent on repeated amount of clicks.
Below is the complete program:
Python3
import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char = 'a' )
stop_key = KeyCode(char = 'b' )
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 = = stop_key:
click_thread.exit()
listener.stop()
with Listener(on_press = on_press) as listener:
listener.join()
|
Now let’s execute the python program we’ve written and then press the start (a) and stop (a) keys in order to initiate the auto clicker.
Output: