There's more...

Occasionally, you might want to register a button press as a single event regardless of whether the user holds the button down for a long or short period of time. This recipe achieves that goal by keeping track of the previous state of the button and only calling the event handler once, as the result of a button press.

Even though you have the extra step of tracking the last state of a button, the benefit of this approach is that you don't have to fiddle with the timing of delays between polling for key presses, or the timing of repeated keyboard delays. This recipe is just another viable approach to solve the problem of when and how to respond to physical button interactions.