pynput鼠标侦听器冻结输入

发布于 2025-01-23 20:02:50 字数 1405 浏览 5 评论 0原文

我正在制作一个程序,该程序通过持有某个鼠标按钮(使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文