user2368631
user2368631

Reputation: 107

python mouse.is_pressed always returns true

Could someone please help me understand why this bit doesn't work as expected?

def X2_buttonDown():
    startedAt = round(time.time() * 1000)
    buttonIsDown = mouse.is_pressed(mouse.X2)

    while buttonIsDown and (round(time.time() * 1000) - startedAt) <= 2000:
        print("still down. sleeping")
        time.sleep(0.1)
        buttonIsDown = mouse.is_pressed(mouse.X2)
        print(mouse.is_pressed(mouse.X2))

    if buttonIsDown:
        print("button was held")
    else:
        print("button was let go before 2 secs")

mouse.on_button(X2_buttonDown, args = (), buttons = [mouse.X2], types = [mouse.DOWN])     

I'm adding a hook on the mouse button down event and I want to detect how long it's been pressed.

I expect is_pressed() to return true when I'm holding the button down. If I release before the 2 seconds then it should return false and update buttonIsDown so it breaks out of the loop; however, it's always returning True even after releasing it.

I think the mouse down event of x2 runs synchronously so is_pressed('x2') is always going to return True, but I guess I'm looking for confirmation.

If I split this up in Down and Up event with a global timer then it's fine, but I wanted to avoid splitting this up.

This is a bit of my output and I simply did a quick click.

still down. sleeping
True
still down. sleeping
True
still down. sleeping
True
still down. sleeping
True
still down. sleeping
True
button was held

Upvotes: 0

Views: 56

Answers (0)

Related Questions