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
|
|
|
|
|
|
|
Stop running a function when button |
|
Run a function every time a button is pressed. |
|
|
|
|
Module Contents
- hub.buttons.center() → bool[source]
Truewhile 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.off(name: _ButtonName) → None[source]
Stop running a function when button
nameis 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.
nameis"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). PassingNoneinstead of a function removes it again.