hub.buttons

The hub’s buttons: a 5-way pad (up, down, left, right and center).

Ask whether a button is held right now with hub.buttons.left() and friends, or have a function run every time a button is pressed with on().

Holding the center button for 2 seconds stops the running program; holding it for 5 seconds turns the hub off.

Functions

center() → bool

True while the center (power) button is held down.

down() → bool

True while the down button is held down.

left() → bool

True while the left button is held down.

off(name) → None

Stop running a function when button name is pressed.

on(…)

Run a function every time a button is pressed.

right() → bool

True while the right button is held down.

up() → bool

True while the up button is held down.

Module Contents

hub.buttons.center() → bool[source]

True while the center (power) button is held down.

Holding it for 2 seconds stops your program, and holding it for 5 seconds turns the hub off.

hub.buttons.down() → bool[source]

True while the down button is held down.

hub.buttons.left() → bool[source]

True while the left button is held down.

hub.buttons.off(name: _ButtonName) → None[source]

Stop running a function when button name is pressed.

hub.buttons.on(name: _ButtonName) → Callable[[_ButtonCb], _ButtonCb][source]
hub.buttons.on(name: _ButtonName, cb: _ButtonCb | None) → _ButtonCb | None

Run a function every time a button is pressed.

name is "center", "up", "down", "left" or "right". The function runs once per press, in its own new thread, while the rest of your program keeps going. Pressing again before it finishes starts another copy alongside the first.

Stop every callback that is still running with hub.stop_callbacks(). They are also stopped when your program ends.

Put it above a function:

@hub.buttons.on("left")
def left_pressed():
    print("left!")

or pass the function directly: hub.buttons.on("left", left_pressed). Passing None instead of a function removes it again.

hub.buttons.right() → bool[source]

True while the right button is held down.

hub.buttons.up() → bool[source]

True while the up button is held down.