CircuitPython HID Keyboard

One of the things we baked into CircuitPython is 'HID' control - Keyboard and Mouse capabilities. This means a Trinket or Gemma can act like a keyboard device and press keys, or a mouse and have it move the mouse around and press buttons. This is really handy because even if you cannot adapt your software to work with hardware, there's almost always a keyboard interface - so if you want to have a capacitive touch interface for a game, say, then keyboard emulation can often get you going really fast!

You'll need to install the adafruit_hid bundle which comes with Keyboard, Keycode and Mouse support

Then try running this example code which will create 3 'buttons' on three Trinket or Gemma pins

# CircuitPlayground demo - Keyboard emu

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 pullup
buttonpins = [board.D2, board.D1, board.D0]
# our array of button objects
buttons = []
# The keycode sent for each button, will be paired with a control key
buttonkeys = [Keycode.A, Keycode.B, "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/pullups
for pin in buttonpins:
    button = DigitalInOut(pin)
    button.direction = Direction.INPUT
    button.pull = Pull.UP   

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
print("Waiting for button presses")

while True:
    # check each button
    for button in buttons:
        if not button.value:   # pressed?
            i = buttons.index(button)
            print("Button #%d Pressed" % i)

            # turn on the LED
            led.value = True

            while not 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:
      , k) # press...
                kbd.release_all()        # release!

            # turn off the LED
            led.value = False

Touch any of the digital IO pads to ground using a wire 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, ...) 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.

For more detail check out the documentation at
Last updated on 2017-12-01 at 04.59.09 PM Published on 2017-08-23 at 05.57.02 PM