Pico RGB keypad with micropython HID

The Pimoroni examples for using the Pico RGB keypad as a HID device only shows using Circuit Python and its Micropython demo just shows using the keypad as a colourful push button device.

This is probably because the micropython HID module for the Pico was not in created at the time the Pico keypad was introduced, though the mp HID module has been available for quite some time now. Theres nothing wrong with using Circuit Python of course, but I wanted to see if I could use the mp HID

Theres a dearth of examples of using mp HID so I show a short example of using this module on a Pico. The mp HID module has to be installed as its not part of the standard mp installation. I installed it on a Pico2W thus making it easy to install with the mp ‘mip’ module that is part of the standard mp installation for ‘W’ modules. An example of the install process is

  1. connect the Pico W to wifi.

  2. at the REPL:

    import mip

    mip.install(“usb-device-keyboard”)

Then the usb library will be installed into the PicoW’s lib folder as .mpy files. Its probably helpful to read the .py files and the keyboard.py file can be found at:

https://github.com/micropython/micropython-lib/blob/master/micropython/usb/usb-device-keyboard/usb/device/keyboard.py

Within the keyboard.py file a ‘class KeyCode:’ can be found where class variables are assigned to various keycode values.

The example below makes the Pico into a HID device and fires off a couple of key codes to whatever the Pico’s usb is attached to when its powered up. (hopefully something like a terminal session on a computer :) )

Do note that if the code is run with the likes of a Thonny IDE, when the program is run, thus turning the Pico into a HID device, the IDE link to the pico is severed. However the program can be coded up and tested in Thonny by commenting out the

usb.device.get().init(kb, builtin_driver=True)

code line and perhaps testing the keycode being delivered with print statements. To then run the code, remove power from the Pico and then restore it again and it will then be a HID device. Clicking the Thonny Stop/Restart button a couple of times will restore the IDE link to the Pico again.

Also note a small delay in the code to allow the Pico, now as a usb HID device, to establish its usb connection to its host before sending any keycodes, and that the keycodes are sent to the kp.send_keys(key) function as a list or tuple

Once the use of the mp HID is established then of course its easy to make the Pico RGB keypad keys send the desired keycodes as a HID device. My Pico keypad is programmed to do a double duty of a keyboard HID device, but with some keys sending mqtt messages instead. Now I’ve just got to remember which keys do what, but they are coloured to serve as a reminder and do flash in nice colours when pressed :)

import usb.device
from usb.device.keyboard import KeyboardInterface, KeyCode
import time

kb = KeyboardInterface()
# the following will make the pico's usb port into a Keyboard device and
# Thonny will loose its link to the pico 
# - comment out when creating program and test with print(key)
usb.device.get().init(kb, builtin_driver=True)

def sendkey(key):
    #print(key)
    kb.send_keys(key)
    kb.send_keys([])  

# Examples of KeyCode charaters which must be passed to 
# kb.send_kyes(key) as a list or tuple.
key_1 = [KeyCode.N1]
key_star = [KeyCode.KP_MULTIPLY]
key_a = [KeyCode.A]
key_A = [KeyCode.LEFT_SHIFT, KeyCode.A]
# to get '#' character for mac uk keyboard
key_hash = [KeyCode.LEFT_ALT, KeyCode.N3]


# Send KeyCodes
# without a small sleep the following keycodes will not get sent
time.sleep(1) 

sendkey(key_1)
sendkey(key_star)
sendkey(key_a)
sendkey(key_A)
sendkey(key_hash)



1 Like

I’ll be tagging along on this one. I have a Pico RGB Keypad Base on my desk coded up in Circuit Python. Everything else I have is Python or Micro Python. I also have a spare keypad I can flash with Micro Python and tinker with the code you posted. =)