'Pynput mouse listener freezing input
I am making a program that toggles on and off by a holding a certain mouse button(using pynput). The problem is that after I start the code and hold the mouse button, while the class is being executed, there is lag in the movement of the cursor. How to fix the lag?
import time
import threading
from pynput import mouse
from pynput.mouse import Button, Controller
delay = 0.01
button = Button.left
control = Controller()
running = False
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 run(self):
while self.program_running:
while self.running:
control.click(self.button)
time.sleep(self.delay)
time.sleep(0.1)
def process():
click_thread = ClickMouse(delay, button)
click_thread.start()
while running:
click_thread.start_clicking()
click_thread.stop_clicking()
def on_click(x, y, button, pressed):
global running
if button == mouse.Button.x2:
if pressed:
if not running: # to run only one `process`
running = True
threading.Thread(target=process).start()
else:
running = False
with mouse.Listener(on_click=on_click) as listener:
listener.join()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
