【问题标题】:How to monitor the mouse doubleclick in pynput?如何在pynput中监控鼠标双击?
【发布时间】:2020-12-19 21:38:30
【问题描述】:

在监控过程中双击时遇到问题。“谁能给我一个代码,如何使用pynput在python中监控鼠标双击?”

【问题讨论】:

  • 您始终可以在单击按钮时节省时间,并将其与下一次单击的时间进行比较 - 如果差异足够小,则您可以双击。问题是当您还想监视单击时 - 因为它需要运行一些函数来始终检查第一次单击后的时间以及是否没有下一次单击然后将其视为单击。

标签: python automation pynput


【解决方案1】:

当您想同时获得single click 时,没有简单的方法获得double 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, button, (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, y, button, pressed):
    global previous_left
    global previous_right
    global previous_middle
    
    #text = 'Pressed' if pressed else 'Released'
    #print('{0} {1} at {2}'.format(text, button, (x, 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 没有点击,则将其视为单击。

我没有这种情况的例子。

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2018-06-07
    • 1970-01-01
    • 2021-07-02
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2010-12-10
    相关资源
    最近更新 更多