Skip to content

USB HID Keyboard

Overview

The USB HID Keyboard sample demonstrates using the USB Human Interface Device (HID) module to implement a keyboard input device that you can connect to your computer.

This sample code enumerates the iMX RT1011 Nano Kit into a HID keyboard that has an A key connected to the USR/BT button.

Tip

adafruit_hid is pre-built into CircuitPython as a frozen module, so that it can be imported in the code directly.

Requirements

Before you start, check that you have the required hardware and software:

Running the code

To run the code, complete the following steps:

  1. Connect iMX RT1011 Nano Kit to your computer using the USB-C Cable.
  2. Start Mu Editor, click Load to open code.py in the CIRCUITPY drive.
  3. Copy and paste the following code into code.py and click Save:

    CIRCUITPY/code.py
    import time
    import board
    import digitalio
    import usb_hid
    from adafruit_hid.keyboard import Keyboard
    from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
    from adafruit_hid.keycode import Keycode
    
    # USR/BT button acts as A key on a keyboard
    key_a = digitalio.DigitalInOut(board.USR_BTN)
    key_a.direction = digitalio.Direction.INPUT
    key_a.pull = digitalio.Pull.DOWN
    
    # Red LED indicates A key is pressed
    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT
    
    # The keyboard object!
    time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
    keyboard = Keyboard(usb_hid.devices)
    keyboard_layout = KeyboardLayoutUS(keyboard)
    
    print("Waiting for key pin...")
    
    while True:
        if not key_a.value:
            print("Key A pressed!")
    
            # Turn on Red LED
            led.value = True
    
            while not key_a.value:
                pass    # Wait for key A released
    
            # Type keycode `Shift+A`
            keyboard.press(Keycode.SHIFT, Keycode.A)
            keyboard.release_all()
    
            # Turn off Red LED
            led.value = False
    
        time.sleep(0.01)
    
  4. Your code will run as soon as the file is done saving. The board will enumerate as a HID keyboard. Click Serial on Mu Editor's Top Menu to open a serial console. You should see the console output, similar to what is shown in the following:

    1
    2
    3
    Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
    code.py output:
    Waiting for key pin...
    
  5. Open a text editor and press USR/BT button on the board. Every button press sends a character A to the computer, and this will be displayed in the text editor.