Jmilicev
Jmilicev

Reputation: 73

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.

Upvotes: 2

Views: 42648

Answers (3)

Hacker--Rohan Raj
Hacker--Rohan Raj

Reputation: 79

This might not be as you wanted but it starts clicking when you press and release 's', and stops clicking when you press and release 's'. if you want to exit the script, press 'e'

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()

Upvotes: 1

El Barae Tounsi
El Barae Tounsi

Reputation: 1

It is a nice question, but I think you can do:

import win32api, win32con
import time

click_speed = int(input("Put your cps here"))
speed = 1 / click_speed
def click(x,y):
    time.sleep(speed)
    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)

Upvotes: 0

arshovon
arshovon

Reputation: 13651

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.

Upvotes: 10

Related Questions