pynput鼠标侦听器冻结输入
我正在制作一个程序,该程序通过持有某个鼠标按钮(使用pynput)打开和关闭。问题在于,在执行类时,我启动代码并按住鼠标按钮后,光标的移动滞后。如何修复滞后?
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论