Simple Python Autoclicker

Simple Python Autoclicker

·

3 min read

Hello everyone! Today we will learn how to create a simple autoclicker with Python. We will look at libraries to manage time, clicks, and keyboard buttons.

The Logic

To start, let me clarify our plan. Our autoclicker will provide options to choose the time interval between clicks and the type of click. By type of click, I mean the selection between using the left or right mouse button, and whether it will be a single or multiple click.

Our code will have 3 functions:

  1. click(): defines the type of click (left/right, single/multiple);

  2. startAutoclicker(): the core of the autoclicker, the function "that will click";

  3. main(): the main function in which we set the delay and the click method.

The Libraries

As I said before we need libraries to handle the keyboard the GUI (the display) and the time delay. To do this we simply add these libraries at the top of our file:

import pyautogui
import time
import keyboard

The click function

Now, we will create our click() function with two parameters: the method (left/right, etc.) and the coordinates of the click:

def click(met, x_pos, y_pos):
    if met == "left":
        pyautogui.click(x_pos, y_pos)
    elif met == "right":
        pyautogui.click(button="right", x=x_pos, y=y_pos)
    elif met == "two-clicks":
        pyautogui.click(clicks=2, x=x_pos, y=y_pos)

The startAutoclicker function

Our autoclicker will work by first taking the x and y positions, then calling the click function, and lastly adding the time delay. You'll see a variable called clickTime in the time.sleep method, we'll create this global variable later in the project:

def startAutoclicker():
    while not keyboard.is_pressed('esc'):
        x, y = pyautogui.position()
        click(method, x, y)
        time.sleep(clickTime / 1000)

To end the autoclicker we simply press the ESC button on the keyboard.

The main function

In the main function, we'll ask what time delay the user wants and what method the autoclicker needs to use:

def main():
    global clickTime, method

    while not keyboard.is_pressed('esc'):
        print("Welcome to the Autoclicker, before using it you need to setup some things (press ESC to stop the program)")
        clickTime = int(input(
            "Select the time between the clicks in ms (leave blank for 1000): ") or 1000)

        method = input(
            "Now select the method of the click between Right Btn./Left Btn./Two Clicks ('left', 'right', 'two-clicks', leave blank = 'left'): ") or 'left'
        if method not in ['right', 'left', 'two-clicks']:
            print("Error: cannot detect method")
            return

        print("All good, Autoclicker started. Press ESC in the terminal to block it!")
        startAutoclicker()

    print("Program Blocked")

To end all we call the main function:

main()

Usage & Resources

To utilize it, just execute the file with Python or, if preferred, create an executable file from it. Please be mindful of the time delay, as using small numbers may result in crashing.

You can find the GitHub repo at this link.


I hope you enjoyed this new type of content. Please feel free to let me know if there are any aspects of my blog that I could improve upon as I am open to constructive criticism. Looking forward to seeing you on the next post. Take care!