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()

    if not pressed:
        releaseTime = time()

    print(pressTime - releaseTime)

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

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).

1 Answers

Community On

You need used global variable

# declare two variable
pressTime, releaseTime

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