微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何监视鼠标在Pynput中的双击?

如何解决如何监视鼠标在Pynput中的双击?

在监视期间双击时,我遇到了一个问题。”任何人都可以向我提供一个代码,该代码如何使用 pynput 在python中监视鼠标双击?”

解决方法

要获得double click时,没有简单的方法来获得single click


要控制double click(不控制single click),您可以记住previous click的时间并与current click进行比较。如果差异是。 0.3s,则可以将其视为double click

仅适用于左键点击的代码

from pynput.mouse import Listener,Button
import time

previous_left = 0

def on_click(x,y,button,pressed):
    global previous_left
    
    #text = 'Pressed' if pressed else 'Released'
    #print('{0} {1} at {2}'.format(text,(x,y)))

    double_click_left = False
    
    # double click left button
    if pressed and button == Button.left:
        current_left = time.time()
        
        diff_left = current_left - previous_left
        print('diff left:',diff_left)
        
        if diff_left < 0.3:
            print('double click left')
            double_click_left = True
            
        previous_left = current_left
    
    # other code
    
    if double_click_left:
        # Stop listener
        return False

with Listener(on_click=on_click) as listener:
    # ... some code ...
    listener.join()

其他按钮的代码相似

from pynput.mouse import Listener,Button
import time

previous_left = 0
previous_right = 0
previous_middle = 0

def on_click(x,pressed):
    global previous_left
    global previous_right
    global previous_middle
    
    #text = 'Pressed' if pressed else 'Released'
    #print('{0} {1} at {2}'.format(text,y)))

    double_click_left = False
    double_click_right = False
    double_click_middle = False
    
    # double click left button
    if pressed and button == Button.left:
        current_left = time.time()
        
        diff_left = current_left - previous_left
        print('diff left:',diff_left)
        
        if diff_left < 0.3:
            print('double click left')
            double_click_left = True
            
        previous_left = current_left
    
    # double click right button
    if pressed and button == Button.right:
        current_right = time.time()
        
        diff_right = current_right - previous_right
        print('diff right:',diff_right)
        
        if diff_right < 0.3:
            print('double click right')
            double_click_right = True
            
        previous_right = current_right

    # double click middle button
    if pressed and button == Button.middle:
        current_middle = time.time()
        
        diff_middle = current_middle - previous_middle
        print('diff middle:',diff_middle)
        
        if diff_middle < 0.3:
            print('double click middle')
            double_click_middle = True
            
        previous_middle = current_middle

    # other code
    
    if double_click_left:
        # Stop listener
        return False


with Listener(on_click=on_click) as listener:
    # ... some code ...
    listener.join()

但是问题是当您还希望控制单击时,因为它会在第一次单击后运行某些功能0.3s,以通知您不是双击而是单击-它需要线程Timer或它需要要运行一直循环运行的其他线程,会将当前时间与previous_left进行比较,如果previous_left之后没有单击0.3s,则将其视为单击。

我没有这种情况的例子。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。