5 buttonshim single/ held press python problem


I’ve used python to act on single and held press. The problem I’m having is that when I want it to handle the held press it’s also treating this as a single press and actioning that code.
Am I doing something wrong or is this a feature?
Many thanks,


This is half way between a bug and a feature, and definitely not something you’re doing wrong. The good news is that I think there’s a sensible way to fix it in the library.

Let’s look at the A button to figure out what’s going on here.

An “A” press actually has three parts to it. When “A” is pressed, when “A” is held, and when “A” is released. And together this forms one complete “A” press.

Now usually it’s the pressing that’s useful, because that’s the only part we look at when triggering a “press” event.

Unfortunately at the moment that event is triggered, we can’t look into the future and know if the button will be held, so the event fires irrespective of whether the button is then held. This isn’t so much a bug as a necessity. because we want the response to a button press to be fast and efficient in most cases.

For a quick fix, you should instead use the “release” event to handle your single press and your handlers should look something like:

button_a_has_been_held = False

def handle_press(button, state):
    global button_a_has_been_held
    button_a_has_been_held = False

def handle_hold(button, state):
    global button_a_has_been_held
    button_a_has_been_held = True
    # Do held stuff here

def handle_release(button, state)
    if button_a_has_been_held:
    # Do release stuff here *if* the button has not been held

This will keep track of whether or not the “A” button has been held in the “button_a_has_been_held” global variable. If the button has been held long enough to trigger a “held” event, then the release event will exit immediately and not perform your short press action.


Thanks for the comprehensive answer. This has fixed it for me. Works a treat!
Thanks very much,