from pynput.mouse import Listener, Button
import time

# 存储按下时间
press_time = 0

def on_click(x, y, button, pressed):
    global press_time
    if button == Button.left:
        if pressed:
            # 记录按下时间
            press_time = time.time()
        else:
            # 计算持续时间并打印
            duration = time.time() - press_time
            print(f"Mouse left button was held for {duration:.4f} seconds")

# 设置监听器
with Listener(on_click=on_click) as listener:
    listener.join()
最后修改:2024 年 06 月 20 日
如果觉得我的文章对你有用,请随意赞赏