Datasheet
Press Button A or Button B to have the keypresses sent.
The Keyboard and Layout object are created, we only have US right now (if you make other layouts please submit a
GitHub pull request!)
# the keyboard object!
kbd = Keyboard()
# we're americans :)
layout = KeyboardLayoutUS(kbd)
Then you can send key-down's with kbd.press(keycode, ...) You can have up to 6 keycode presses at once. Note that
these are keycodes so if you want to send a capital A, you need both SHIFT and A. Don't forget to call kbd.release_all()
soon after or you'll have a stuck key which is really annoying!
You can also send full strings, with layout.write("Hello World!\n") - it will use the layout to determine the keycodes to
press.
# make all pin objects, make them inputs w/pulldowns
for pin in buttonpins:
button = DigitalInOut(pin)
button.direction = Direction.INPUT
button.pull = Pull.DOWN
buttons.append(button)
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
print("Waiting for button presses")
while True:
# check each button
# when pressed, the LED will light up,
# when released, the keycode or string will be sent
# this prevents rapid-fire repeats!
for button in buttons:
if button.value: # pressed?
i = buttons.index(button)
print("Button #%d Pressed" % i)
# turn on the LED
led.value = True
while button.value:
pass # wait for it to be released!
# type the keycode or string
k = buttonkeys[i] # get the corresp. keycode/str
if isinstance(k, str):
layout.write(k)
else:
kbd.press(controlkey, k) # press...
kbd.release_all() # release!
# turn off the LED
led.value = False
time.sleep(0.01)
© Adafruit Industries https://learn.adafruit.com/adafruit-circuit-playground-express Page 149 of 211










