python

Creating a basic auto clicker in python


I want to create a pretty simple automatic-clicker that will take the position of your mouse, and clicks in that position as long as "active" is true and at "input" 's speed (clicks per second)

I have seen this code floating around, but it does not suit my needs

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

I would like the code to look something like this,

import library 

input = 5 ## in clicks per second

if (some key) is held:
    active = True

if (some key) is released:
    active = False


while active:
     *gets position of mouse*
     *clicks at that position at input's speed (5)

It would also be nice if there was an option to click in the very center of the screen.


Solution

  • Try using pyautogui. I have used this in several projects. It has huge functionalities with less coding. For example, if you want to click on the middle of the screen, simply do:

    import pyautogui
    width, height = pyautogui.size()
    pyautogui.click(width/2, height/2)
    

    In your case you may use time module to synchronize the actions.

    Here is the cheat sheet of pyautogui.