Create CircuitPlaygroundExpress_HIDKeyboard.py

Circuit Playground Express HID Keyboard demo
This commit is contained in:
John Edgar Park 2017-10-06 15:19:01 -07:00 committed by GitHub
parent 40b2806412
commit 5460f33dc6

View file

@ -0,0 +1,64 @@
# CircuitPlaygroundExpress_HIDKeyboard
from digitalio import DigitalInOut, Direction, Pull
import touchio
import board
import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
# A simple neat keyboard demo in circuitpython
# The button pins we'll use, each will have an internal pulldown
buttonpins = [board.BUTTON_A, board.BUTTON_B]
# our array of button objects
buttons = []
# The keycode sent for each button, will be paired with a control key
buttonkeys = [Keycode.A, "Hello World!\n"]
controlkey = Keycode.SHIFT
# the keyboard object!
kbd = Keyboard()
# we're americans :)
layout = KeyboardLayoutUS(kbd)
# 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 type(k) is 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)