-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script Continues Rapid Clicking Despite Mouse Button Release (is_pressed Functionality Issue) #588
Comments
UPDATE I have verified that xdotool does send clicks when isolated. Attached is a link of several hours of debugging with Chatgpt. https://chat.openai.com/share/96402fab-772f-4ca1-8b95-c1646522d51d The terminal reads the events correctly. I am having trouble integrating the isolated xdotool clicks with Listener. I suspect the problem might be with Listener. The current code below does not send any left clicks. Sometimes Chatgpt makes it where it keeps clicking after the first initial click but it does not stop after the loop begins.
|
Made a post here seeking help. https://www.reddit.com/r/learnpython/comments/1bk5pqw/pynput_script_so_close_to_working_please_help_btc/ |
Thank you for your report. I think the problem stems from this simple fact: every click you send with a virtual pointing device---either If you install pynput from this branch, you might be able to modify you code thus: #!/usr/bin/env python3
from pynput.mouse import Listener, Controller, Button
import time
# Constants
CLICK_DELAY = 0.05
# Global variable to track the state of the left mouse button
is_pressed = False
# Function to perform rapid clicking
def rapid_clicking():
# Please note that this will also need to be changed: as documented
# (https://pynput.readthedocs.io/en/latest/mouse.html#the-mouse-listener-thread), the callback must
# not block, so this must be delegated to a separate thread
mouse = Controller()
while is_pressed:
mouse.click(Button.left)
time.sleep(CLICK_DELAY)
# Callback function for mouse events
def on_click(x, y, button, pressed, injected):
global is_pressed
if button == Button.left and not injected:
is_pressed = pressed
if pressed:
rapid_clicking()
# Start listening to mouse events
with Listener(on_click=on_click) as listener:
listener.join() Please give it a try! If you report back success, I will merge the feature branch. |
Here are some of my attempts to do this.
I tested this code and the terminal read correctly but no clicks were sent.
I tried implementing your comment about the callback being delegated to a separate thread. No clicks where sent when the code below was ran.
|
you could try the evdev library, the same library that pynput uses but you could select an physic devices to listen instead of listen global events from any devices could make something like that: #!/usr/bin/env python3
from pynput.mouse import Listener, Controller, Button
import time
import threading
import evdev
# Constants
CLICK_DELAY = 0.04
# Global variable to track the state of the left mouse button
is_pressed = False
dev = evdev.InputDevice('/dev/input/event6')
# Function to perform rapid clicking
def rapid_clicking():
mouse = Controller()
while is_pressed:
mouse.click(Button.left)
print("Sending a left click...")
time.sleep(CLICK_DELAY)
# Callback function for mouse events
def on_click(pressed):
global is_pressed
is_pressed = pressed
try:
thread = threading.Thread(target=rapid_clicking)
thread.start()
except Exception as e:
print(f"Error occurred in callback function: {e}")
for event in dev.read_loop():
event:evdev.InputEvent
if event.type==1 and event.value==1 and event.code==272:
on_click(True)
elif event.type==1 and event.value==0 and event.code==272:
on_click(False) the unique problem is that when you pressing the button, the click can't not be overwritten |
or even better you could uses evdev event llistener and using #!/usr/bin/env python3
import time
import threading
import evdev
# Constants
CLICK_DELAY = 0.04
# Global variable to track the state of the left mouse button
is_pressed = False
dev = evdev.InputDevice('/dev/input/event6')
ui = evdev.UInput.from_device(dev)
# Function to perform rapid clicking
def rapid_clicking():
while is_pressed:
ui.write(1, 272, 1)
ui.syn()
ui.write(1, 272, 0)
ui.syn()
print("Sending a left click...")
time.sleep(CLICK_DELAY)
# Callback function for mouse events
def on_click(pressed):
global is_pressed
is_pressed = pressed
try:
thread = threading.Thread(target=rapid_clicking)
thread.start()
except Exception as e:
print(f"Error occurred in callback function: {e}")
dev.grab()
for event in dev.read_loop():
event:evdev.InputEvent
if event.type==1 and event.value==1 and event.code==272:
on_click(True)
elif event.type==1 and event.value==0 and event.code==272:
on_click(False)
else:
ui.write_event(event) |
THE SCRIPT: Essentially, the functionality is to rapidly click WHILE (AND ONLY WHILE) the physical left mouse button is held DOWN and stops clicking immediately when the physical left mouse button enters the UP position.
Description
This Python script utilizes the
pynput
library to listen for mouse events, specifically the left mouse button clicks. Here's a breakdown of what the script does:Import necessary modules:
Listener
: frompynput.mouse
, for listening to mouse events.Controller
: frompynput.mouse
, for controlling the mouse.Button
: frompynput.mouse
, for identifying mouse buttons.time
: for adding delays.Define constants:
CLICK_DELAY
: Specifies the delay between each click.Define a global variable:
is_pressed
: Tracks the state of the left mouse button (whether it's pressed or released).Define a function
rapid_clicking()
:CLICK_DELAY
.Define a callback function
on_click()
:is_pressed
based on the state of the left mouse button.rapid_clicking()
function.Start listening to mouse events using
Listener
:on_click()
function whenever a mouse button is clicked.with
statement ensures that theListener
is properly closed after execution.Overall, this script should allow for rapid clicking with the left mouse button while it is held down and stops clicking when the button is released.
Platform and pynput version
Linux Mint 21.3
Cinnamon v6.0.4
Kernel: 6.5.0-21-generic
display-server: X11
pynput-Version: 1.7.6
This script is my attempt at converting the following Autohotkey script to work on Linux (so far, of all the linux-ahk like macroing distros, the above script is as close to the desired functionality of the following):
ISSUE: The mouse successfully and continuously sends clicks when the left mouse button is DOWN. The problem is, the script fails to temporarily pause when the left mouse button is in the UP position. The script does not stop clicking after the first initial click. I need the script to be able to start and temporarily stop easily without the script inside the terminal being stopped completely.
The text was updated successfully, but these errors were encountered: