How it works...

The button_change function is defined, which will be called each time the state of the button changes.

The last global variable will be used to keep track of the last state of the button. Then, an infinite loop is started, which will check whether the current state of the button differs from its last state. If it has detected a change, then it will call the button_change function.

Finally, the latest button state is saved in the last variable whenever it changes. This script essentially implements an event loop that detects button press events and calls the button_change event handler to handle these events whenever they are detected.