Sitelet https://github.com/pardeep2690/pico-hidkbd
Skip to content

Repository files navigation

pico-hidkbd

License: MIT MicroPython tests

Plug-and-play USB HID keyboard library for MicroPython on Raspberry Pi Pico W (also Pico and Pico 2).

from hidkbd import Keyboard, Key

kb = Keyboard()                 # USB keyboard; REPL stays connected
kb.write("Hello Pico W!\n")     # type a string
kb.hotkey("ctrl", "c")          # named combos
kb.tap(Key.ENTER)

No CircuitPython. No custom UF2. Copy two folders onto the Pico and import.

Why this exists

  • CircuitPython adafruit_hid is easy, but it is not MicroPython.
  • Official MicroPython usb-device-keyboard only sends raw keycode tuples. There is no write("text").
  • Older Pico HID forks need a custom firmware image.

This repo is the missing high-level API on stock MicroPython.

Requirements

Install

1. Flash MicroPython

Hold BOOTSEL, plug the Pico in, drop the .uf2 onto the RPI-RP2 drive.

2. Copy the library

Put both folders on the Pico under /lib:

/lib/hidkbd/
/lib/usb/

Thonny (Windows): interpreter = MicroPython (Raspberry Pi Pico). Upload hidkbd and usb into /lib. Do not upload __pycache__.

mpremote:

pip install mpremote
git clone https://github.com/pardeep2690/pico-hidkbd.git
cd pico-hidkbd
python tools/install_to_pico.py

mip (needs network on the Pico, or mpremote mip):

mpremote mip install github:pardeep2690/pico-hidkbd

3. First run

from hidkbd import Keyboard
kb = Keyboard()
kb.write("hello\n")

Thonny / serial disconnects once while USB re-enumerates as a keyboard. Reconnect. The REPL still works (keep_repl=True by default).

BLE from an Android phone (Pico W)

The Pico stays a USB keyboard. The phone sends keys, strings, or macros over BLE.

python tools/install_to_pico.py --ble

Run ble_demo.py. On the phone install Serial Bluetooth Terminal, connect to PicoHID, then send:

hello from android
/k enter
/k ctrl+c
/m hello

Full steps: docs/BLE.md.

Optional USB-only demo (hold BOOTSEL to type, no extra wiring):

python tools/install_to_pico.py --demo

Then run hid_demo.py on the Pico. Do not replace main.py until you are ready — HID on boot makes Thonny disconnect every power-up.

Full step-by-step: docs/INSTALL.md.

API

Type text

from hidkbd import Keyboard

kb = Keyboard()
kb.write("user@host\n")
kb.writeln("next line")

Unsupported characters raise ValueError.

Named keys and combos

kb.tap("enter")
kb.tap("f5")
kb.hotkey("ctrl", "c")
kb.hotkey("ctrl+alt+delete")
kb.hotkey("win", "r")

Names: ctrl, shift, alt, win / cmd / gui, enter, esc, tab, space, backspace, delete, arrows, home / end, pageup / pagedown, f1–f24, and characters like "a" or "!".

Hold / release

from hidkbd import Key

kb.press(Key.SHIFT)
kb.tap(Key.A)
kb.release(Key.SHIFT)
kb.release_all()

Media keys

Must be enabled on the first Keyboard() call:

kb = Keyboard(media=True)
kb.media.volume_up()
kb.media.mute()
kb.media.play_pause()

Keyboard() options

Option Default Meaning
wait True Block until the PC enumerates the keyboard
keep_repl True Keep the MicroPython serial port. Leave this True
media False Add volume / play HID interface
delay_ms 8 Pause between characters in write()
hold_ms 12 How long a tap holds the key down
settle_ms 800 Extra wait after USB comes up
timeout_ms 15000 Give up waiting for the host

LED state from the host:

print(kb.caps_lock, kb.num_lock, kb.scroll_lock)

Examples

See examples/README.md.

File What it does
examples/00_bootsel_hello.py Hold BOOTSEL to type a test string
examples/01_hello.py Type a string
examples/02_hotkeys.py Combos and arrows
examples/03_gpio_button.py Button on GP15 → type a line
examples/04_wifi_type.py Pico W Wi-Fi AP; type from a phone
examples/05_media_keys.py Volume / play-pause

How it works

  1. Keyboard() registers a USB HID boot keyboard (optional media keys) using machine.USBDevice.
  2. builtin_driver=True keeps CDC, so Thonny and mpremote still work.
  3. USB re-enumerates once. Windows / macOS / Linux see a standard keyboard. No drivers.

usb/ is the official MicroPython usb.device stack (MIT). hidkbd/ is the high-level API.

Troubleshooting

Thonny died on first run. Expected. USB re-enumerated. Stop/Restart and reconnect.

Nothing types. Focus a text editor, wait for the Pico LED, then hold BOOTSEL or kb.write("hi").

Wrong characters (@ becomes "). Host OS is not US QWERTY. Switch the PC layout.

REPL gone forever. You passed keep_repl=False. Hold BOOTSEL, re-flash MicroPython, copy files again. Never put keep_repl=False in boot.py.

machine.USBDevice missing. Firmware is too old. Flash MicroPython 1.23+.

no module named 'usb.device'. usb/ is not on the Pico under /lib.

Close Thonny before mpremote. Windows will not share the serial port.

Development

python -m unittest discover -s tests -v

Tests cover key names and the US layout on CPython (no Pico required).

License

MIT. See LICENSE.

USB device support in usb/ is vendored from micropython-lib (MIT, Angus Gratton). See NOTICE.

About

Plug-and-play USB HID keyboard library for MicroPython on Raspberry Pi Pico / Pico W

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages