python - fastest way to click mouse - python

I'm trying to make an auto click bot in python using pyautogui but this takes too much time (it runs in a loop, and xPos/yPos changes every time). What faster ways are there? Thanks for helping.
pyautogui.PAUSE = 0.001
pyautogui.click(xPos,yPos,button = 'left')

You can use pynput:
from pynput import mouse
from pynput.mouse import Controller, Button
import time
mouse = Controller()
one = time.time_ns()
for i in range(1000):
mouse.click(Button.left)
two = time.time_ns()
print(two-one)
With this setup im able to execute 1000 clicks in .53 seconds.

I'm testing three library: Pyautogui, Mouse and Pynput.
Links to libs:
Pyautogui last upd. OCT 2021
Mouse last upd. APR 2021
Pynput last upd. OCT 2021
Below code and result:
PYAUTOGUI version:
import pyautogui
def click_pyautogui(x, y, button):
pyautogui.moveTo(x, y)
pyautogui.click(button=button)
MOUSE version:
import mouse
def click_mouse(x, y, button):
mouse.move(x, y, absolute=True)
mouse.click(button=button)
PYNPUT version:
from pynput.mouse import Button, Controller
def click_pynput(x, y, button):
mouse = Controller()
mouse.position = (x, y)
button = Button.left if button=='left' else Button.right
mouse.click(Button.left)
wrapper:
def click(x, y, button):
# pyautogui
# click_pyautogui(x, y, button)
# mouse
# click_mouse(x, y, button)
# pynput
click_pynput(x, y, button)
import timeit
if __name__ == '__main__':
print(timeit.timeit("click(random.randrange(100), random.randrange(100), 'left')", number=100, globals=locals()))
RESULTS time for 100 cycles (average of 3, very small variations):
Pyautogui: 22.07 sec
Mouse : 0.16 sec
Pynput : 0.20 sec
Mouse look as fastest library!

I can't comment so I have to make a post, I just wanted to say that #swasher 's speed test is incorrect.
from pynput.mouse import Button, Controller
def click_pynput(x, y, button):
mouse = Controller()
mouse.position = (x, y)
button = Button.left if button=='left' else Button.right
mouse.click(Button.left)
This creates a new Controller object every time click_pynput is called, which is unnecessary and slow.
Creating the Controller object once before the function declaration is much better:
from pynput.mouse import Button, Controller
_mouse = Controller()
def click_pynput(x, y, button):
_mouse.position = (x, y)
button = Button.left if button=='left' else Button.right
_mouse.click(Button.left)
This in fact showed (on my pc) that pynput and mouse have the same speed.
Real RESULTS time for 100 cycles (average of 3, very small variations):
Mouse : 0.1317509999498725 sec
Pynput : 0.1323150999378413 sec
Also I tested just move speed and pyinput is slightly faster (1000000 iterations):
Mouse : 24.206686099991202 sec
Pynput : 20.718958700075746 sec

Related

Pynput sent mouse only press and won't release

Hi I wanted to bind extra buttons on my mouse using pynput for playing a game
Button.x1 for spam spacebar key
Button.x2 for spam left mouse
and all of them is trigger
for x1 button is worked. but the x2 is more likely holding the left mouse button, when I activate it
I've tried run the script on terminal (as admin). but still won't work
here is my code
from pynput.mouse import Listener, Button, Controller as MController
from pynput.keyboard import Key, Controller as KController
from time import sleep
from threading import Thread
from random import uniform
C_keyboard = KController()
C_mouse = MController()
state = [False, False]
def bind(i, key, c):
global state
while state[i]:
c.press(key)
sleep(round(uniform(0.5, 1.0), 10))
c.release(key)
def onClick(x, y, button, pressed):
global state
global C_keyboard
global C_mouse
if pressed:
if button == Button.x1:
state[0] = not state[0]
Thread(target=bind, args=(0, Key.space, C_keyboard)).start()
elif button == Button.x2:
state[1] = not state[1]
Thread(target=bind, args=(1, Button.left, C_mouse)).start()
print("%s is %s (%s, %s)" % (button, ("Pressed" if pressed else "Released"), state[0],state[1]))
def main():
with Listener(on_click=onClick) as listener:
listener.join()
Thread(target=main).start()
here is the output of my onClick function when I pressed x2 once

Error when running a simple pynput program

I'm making a project and a part of it is making a program that when activated, automatically right clicks whenever I left click. But when I launch the program and left click, it returns an error:
TypeError: combatModules.on_click() takes 4 positional arguments but 5 were given
My code: (I'm using threads so i can have multiple programs running at once from the same program.)
import pydirectinput as pa
import time as t
import pynput
import threading
class combatModules:
def __init__(self) -> None:
pass
def on_click(x, y, button, pressed):
if button == pynput.mouse.Button.left:
print('{} at {}'.format('Pressed Left Click' if pressed else 'Released Left Click', (x, y)))
pa.rightClick()
else:
print('{} at {}'.format('Pressed Right Click' if pressed else 'Released Right Click', (x, y)))
def blockHitCode(self):
for i in range(100):
listener = pynput.mouse.Listener(on_click=self.on_click)
listener.start()
listener.join()
def blockHit(self):
blockHitThread = threading.Thread(target=self.blockHitCode)
blockHitThread.start()
A little explanation: blockHit() is meant to be executed from the main program.
Your method is missing the self argument. This is required for methods in a class like you have with the blockHitCode and blockHit.
This should fix the error you are having:
def on_click(self, x, y, button, pressed):

How to make a hold to click autoclicker?

This autoclicker script I found automatically clicks the mouse when you press the "s" key. However, I want to change it so that the autoclicker will only run when the left mouse button is depressed. I have spent a surplus of 10 hours trying to figure this one out, but frankly, I'm terrible at coding. Any help would be great. (Also, there is no GetKeyState or GetASyncKeyState in any library I know of)
My code is:
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
delay = 0.01
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)
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()
you have to go to microsoft to get the Virtual-Key codes for input.
For left mouse buttons, the code is 0x01. for Right mouse buttons the code is 0x02.
You will also need to install pywin32 and import the win32 api
Then you can do something like this:
```while True:
a = win32api.GetKeyState(0x01)
print(a)
time.sleep(255.0) #this should be long enough
Basically, when this is run where there's no input pressed, the script will print 0 indefinitely. However when it is pressed, it will print a 1 for as long as it's pressed.
From there you can make that into an if/then statement and activate the clicking function.

psychopy mouse tracking returns true regardless of mouse click

I am trying psychopy's mouse tracking feature. For starters, I just want to record if a mouse click occurs or not. Even if I don't click the mouse, it's printing "got pressed". I am using v1.84.2.
from psychopy import core, visual, gui, data, misc, event, sound
import time, random, math, os
import numpy as np
import copy
from pygame import mouse, locals, display
screenSize =[1280, 800]
win = visual.Window(size = screenSize, fullscr = True, allowGUI=False, monitor='testMonitor', units='deg')
mouse = event.Mouse(visible=True, newPos=[0,0])
mouse.clickReset()
if mouse.getPressed():
print ("got pressed")
mouse.getPressed() returns a tuple of 3 elements representing what mouse button was pressed, so (1, 0, 0) would indicate the left button is being pressed s your code should look like this:
from psychopy import core, visual, gui, data, misc, event, sound
import time, random, math, os
import numpy as np
import copy
from pygame import mouse, locals, display
screenSize =[1280, 800]
win = visual.Window(size = screenSize, fullscr = True, allowGUI=False, monitor='testMonitor', units='deg')
mouse = event.Mouse(visible=True, newPos=[0,0])
mouse.clickReset()
if mouse.getPressed()[0]:
print ("got pressed")

Print while mouse pressed

i am using PyMouse(Event) for detecting if mouse button is pressed:
from pymouse import PyMouseEvent
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
else:
self.stop()
O = DetectMouseClick()
O.run()
This works so far, but now i want to loop print("click") until mouse isnt pressed anymore ... i tried:
def click(self, x, y, button, press):
if button == 1:
if press:
do = 1
while do == 1:
print("down")
if not press:
do = 0
And also smth. like:
while press:
print("click")
Someone can help me? Thanks!
I think as Oli points out in his comment there isn't a constant stream of clicks when the mouse button is held down so you'll have to have the print in a loop. Having the while loop running on the same thread prevents the click event firing when the mouse is released so the only way I can think of to achieve what you are after is to print("click") from a separate thread.
I'm not a Python programmer but I've had a stab which works on my machine (Python 2.7 on Windows 8.1):
from pymouse import PyMouseEvent
from threading import Thread
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def print_message(self):
while self.do == 1:
print("click")
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
self.do = 1
self.thread = Thread(target = self.print_message)
self.thread.start()
else:
self.do = 0
print("end")
else:
self.do = 0
self.stop()
O = DetectMouseClick()
O.run()

Categories