Control your hub with your keyboard
This project shows how you to use keyboard input in your programs.
How it works
Your MicroPython programs can produce output using the print
command, but
it can also read input. To enter input, just click on the terminal window
and press some keys.
You can read keyboard presses using stdin
, as shown in the example below.
Then you can make your program choose different behaviors based on which key
is pressed.
# Import required MicroPython libraries.
from usys import stdin
from uselect import poll
# Register the standard input so we can read keyboard presses.
keyboard = poll()
keyboard.register(stdin)
while True:
# Check if a key has been pressed.
if keyboard.poll(0):
# Read the key and print it.
key = stdin.read(1)
print("You pressed:", key)
This project was submitted by The Pybricks Team.