Buttons
hub.buttons (hub._buttons_module) covers the 5-way
directional pad (center = power button).
Level readers
Return the current held state:
if hub.buttons.center():
...
if hub.buttons.left():
...
Names: center, up, down, left, right.
Callbacks
Callbacks fire on rising edge (release → press). They only fire
from inside poll(), which the Python main
loop must call. When a script runs under hub.on(...), the runner
polls for you. (In later releases the poll methos is expected to be eliminated.)
Decorator form:
@hub.buttons.on("left")
def left_pressed():
print("<--")
@hub.buttons.on("center")
def center_pressed():
print("center")
Manual form:
hub.buttons.on("up", my_cb) # register
hub.buttons.on("up", None) # unregister
hub.buttons.off("up") # alt: unregister
Manual polling (bare scripts)
import hub, time
while True:
hub.buttons.poll()
time.sleep_ms(10)
Exiting a program
Holding the centre button for >= 2 s triggers an exception that quits the program running. Your Python program cannot suppress this — treat it as a physical kill switch.
To exit from Python: hub.exit() (does not return).
Power-off
Holding the centre button for >= 10 s triggers a hardware power- off in the C loop. Your Python program cannot suppress this — treat it as a physical kill switch.
To power off from Python: hub.powerOff() (does not return).
Callback registry snapshots
_snapshot() and
_restore() swap the registry — used by
the on-device menu when launching a user script so the menu’s own
callbacks do not fire while the user program owns the buttons.