简体   繁体   English

如何测量使用Pynput的鼠标单击所花的时间?

[英]How do I measure the amount of time a mouse click was held for using pynput?

I'm creating an application where I want to measure how long a mouse click was held for. 我正在创建一个应用程序,该应用程序要测量鼠标单击保持多长时间。 I've given it a couple attempts and want to see where I'm going wrong. 我做了几次尝试,想看看我要去哪里。

def on_click(x, y, button, pressed):
    if pressed:
        pressTime = time()
        print(pressTime)

    if not pressed:
        releaseTime = time()
        print(releaseTime)

    print(pressTime - releaseTime)

with mouse.Listener(on_click=on_click) as listener:
    listener.join()

I expect an output of the difference between the two times, but I get an error telling me that I've tried to use a variable without initializing it (either pressTime or releaseTime). 我希望得到两次之间的差异输出,但是我收到一条错误消息,告诉我我尝试使用变量而不对其进行初始化(pressTime或releaseTime)。

You need used global variable 您需要使用global变量

# declare two variable
pressTime, releaseTime

def on_click(x, y, button, pressed):
    global pressTime, releaseTime

    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM