Why does this auto-clicker program stop when it starts clicking? - python

I am in the process of writing an auto-clicker that will work by holding down a button. I've checked, I haven't seen anywhere in the python community an auto-clicker that works on this principle.
When the program detects that I press the left mouse button it automatically starts clicking. Then, it detects this clicking as if it was me clicking so the program stops.
I'd like the program to click with a given frequency so long as the left mouse button is pressed, and to stop when it is released. Is there a way to accomplish this?
import time
import mouse
from pynput.mouse import Button, Controller
start = Controller()
while True:
time.sleep(0.0001)
while mouse.is_pressed("left"):
start.click(Button.left)
time.sleep(0.1)

Related

How to trigger mouse clicks only when a key is pressed ? In Python

I want to make a program or when I click on a key the mouse clicks automatically (as long as I click on the key) if I do not click on the key it stops.
I don't want the clicks to happen only when I touch the key once, but as long as the key is held down (It can also be the left button of the mouse pressed that trigger clicks like razer synapse mouses)
Any Idea ?
EDIT 1 :
This one works but not when a key is held down (even when the click is held down it doesn't work anyway) it only detects a single click on the mouse and then it clicks by itself instead of clicking ONLY when the key is held down...
import pyautogui, time
from pynput import mouse
from pynput.mouse import Button,Controller
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry('500x400')
combo = ttk.Combobox(root,values=['ctrl','shift','alt'],width=5)
combo.set('Key...')
combo.pack()
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
while pressed:
pyautogui.click()
pyautogui.PAUSE = 0.1
else:
return False
with mouse.Listener(
on_click=on_click
) as Listener:
Listener.join()
root.mainloop()
You can use the mouse module (pip install mouse) to setup mouse hooks (hotkeys) that will let you trigger the clicking globally. However, in order to manage the beginning and end of this clicking, you will need to use a new thread (here is a short intro to threading if you want to learn more about it). You will want to start a thread when you press down your hotkey. This thread will continuesly click until you trigger an event that stops it. You will trigger this event by releasing your hotkey. Thus, the thread (and with it the clicking) will begin when you press the hotkey down and end when you let it back up.
Here is a piece of code that does exactly that using the middle (scroll) mouse button as the hotkey:
import mouse # pip install mouse
import threading
import pyautogui
pyautogui.PAUSE = 0.1 # set the automatic delay between clicks, default is 0.1
def repeat_function(kill_event):
# as long as we don't receive singal to end, keep clicking
while not kill_event.is_set():
pyautogui.click()
while True:
# create the event that will kill our thread, don't trigget it yet
kill_event = threading.Event()
# create the thread that will execute our clicking function, don't start it yet
new_thread = threading.Thread(target=lambda: repeat_function(kill_event))
# set a hook that will start the thread when we press middle mouse button
mouse.on_button(new_thread.start, (), mouse.MIDDLE, mouse.DOWN)
# set a hook that will kill the thread when we release middle button
mouse.on_button(kill_event.set, (), mouse.MIDDLE, mouse.UP)
# wait for user to use the hotkey
mouse.wait(mouse.MIDDLE, mouse.UP)
# remove hooks that used the killed thread and start again with a new one
mouse.unhook_all()
If you want to use the right mouse button instead, replace mouse.MIDDLE with mouse.RIGHT. I would not recommend using the left mouse button as the hotkey, as pyautogui will simulate clicking this button and likely break the program. If you want to use a key on the keyboard as the hotkey, check out the keyboard module. The concept there is the exact same.
Note that as this code is implemented, it will not be able to do anything else while waiting for the hotkey and processing it. You will need to use it as a separate python program if you want to use it as-is. You could also implement this code to run in a separate thread during another program, but it would definitely be easier to just launch it as a stand-alone script.

Is there a way to let Python differentiate clicks sent by the program and clicks sent by the user?

for some context, I'm trying to make a python program that left-clicks for you when the left mouse button is held down.
This is how it works: while True:
keystate = win32api.GetAsyncKeyState(0x02) or win32api.GetAsyncKeyState(0x01)
if left_clicker == 'true':
if keystate == win32api.GetAsyncKeyState(0x01):
ac()
if keystate == win32api.GetAsyncKeyState(0x02):
if right_clicker == 'true':
rightac()
The ac() loop clicks and then waits for a delay, and then repeats the loop. However, when I run the code, I hold down the mouse button, it clicks once, and then stops. The reason is the program reads the left click sent by Python and sees that the left mouse button has been released (which must happen upon click or the loop will run indefinitely regardless of how the user interacts with the mouse). So, is there any way I can let Python differentiate clicks sent by the program and directly by the user so it won't stop itself? The only workaround I have found is using a different button to trigger the loop, but that's not really what I'm trying to do. Please ask for clarification if needed
I have tried using Pynput and PyAutoGUI as the method for clicking the mouse, both had the same issue. I don't know of any other libraries that can click the mouse and I'm out of ideas on how to solve this.

How do I use side mouse buttons with Pynput?

I am using the Python module Pynput to make a macro that will press one of my side buttons. Does anyone know what the side buttons are called in Pynput?
For example:
from pynput.mouse import Button, Controller
mouse = Controller()
mouse.press(Button.SIDEBUTTON)
mouse.release(Button.SIDEBUTTON)
What would go in the SIDEBUTTON part?
So this question is a bit old and i had the same problem. I figured out how these buttons are called:
its Button.x1 and Button.x2 for Mouse4 and Mouse5.
Hope I could help you. The script I used to find it is right here:
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
if pressed:
print(button)
# Collect events until released
with Listener(on_click=on_click) as listener:
listener.join()
unfortunately there isn't such a function in pynput and if you run this code and press side mouse buttons you will realize that it wont give you any output:
from pynput import mouse
def on_click(x, y, button, pressed):
if pressed:
print ('Mouse clicked {2}'.format(x, y, button))
with mouse.Listener(on_click=on_click) as listener:
listener.join()
Additional buttons on mouse models are usually communicated as 'Button 6' and 'Button 7', etc. (buttons 4 and 5 are the scroll 'buttons'). Some mouse manufacturers send keyboard codes instead (like multimedia buttons or other custom codes).
For Windows and OS X Pynput only supports the left, right, middle mouse buttons however, so you'd be out of luck on those platforms as far as Pynput is concerned. If you are on Linux (with the X.org back-end), you can send and receive more buttons, from button8 all the way up to button31, as well as scroll_up, scroll_down, scroll_left and scroll_right.
So depending on the mouse model you are using and your operating system, you may be able to get the right events for those buttons, be they mouse buttons or keyboard events. Register both a mouse and a keyboard listener, and print out the button value for on-click events for the mouse, and the key for keyboard press or release events, and see if you can get your side button to show up.
When not on Linux, if the specific mouse buttons are sent as a keyboard events, you are in luck and can use the keyboard controller to send the same events. If not, then Pynput can't send such mouse button events either.
That's not to say you can't send such button clicks at all, but you'd have to study the source code of the controller used for Windows or OSX and then see how the underlying framework would accept other button presses besides left, right and middle.

Detect if mouse is Waiting or Busy using python

I am creating a script in Python 2.7. The script automates mouse clicks, within an application.
There are instances that after a mouse click, the mouse cursor will be "Waiting", I want to wait until the mouse cursor changes back to normal before I go to the next step within the code.
Is it possible within python to detect whether the mouse is waiting?

Python [Canvas]: Detect Mouse Button NOT pressed

My question today is, that I'd like to know, if there's a way to detect a mouse button NOT pressed using canvas.bind().
I want to know that, because I want to save data while a button is pressed, and to stop saving data when the button is not pressed anymore.
I want to do this with the left mouse button / ''
If you don't know what I wan't to do; feel free to ask in the comments :/
Detect mouse press and release events. When one of those events occur, start or stop saving as appropriate.
canvas.bind("<Button-1>", start_saving)
canvas.bind("<ButtonRelease-1>", stop_saving)

Categories

Resources