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.
Contents of Tutorial
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 threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode delay = 0.001 button = Button.left begin_end= KeyCode(char='s') exit_key = KeyCode(char='e') class Mouse_click(threading.Thread): def __init__(self, delay, button): super(Mouse_click, self).__init__() self.delay = delay self.button = button self.run = False self.run_prgrm= True def begin(self): self.run = True def end(self): self.run = False def exit(self): self.end() self.run_prgrm= False def run(self): while self.run_prgm: while self.run: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1) mouse = Controller() click= Mouse_click(delay, button) click_thread.start() def on_press(key): if key == begin_end: if click.run: click.end() else: click.begin() elif key == exit_key: click.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.
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.