Skip to content

Instantly share code, notes, and snippets.

@seancmonahan
Created February 16, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancmonahan/e45643482fe289b2f33bf7fbbf41c4f4 to your computer and use it in GitHub Desktop.
Save seancmonahan/e45643482fe289b2f33bf7fbbf41c4f4 to your computer and use it in GitHub Desktop.
CircuitPython "keyboard" to type in a public key into the Raspberry Pi network installer
'''CircuitPython script that simulates a USB keyboard. It waits 5 seconds, and then
types in the contents of the string `public_key`. I wanted a way to easily enter
my SSH public key without manually typing it into the Raspberry Pi network installer.
Requires `adafruit_hid` from the Adafruit CircuitPython Bundle:
https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases
For a Raspberry Pi Pico running CircuitPython, I used
https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/download/20220216/adafruit-circuitpython-bundle-7.x-mpy-20220216.zip
To install the adafruit_hid package, extract the zip, and then
copy the `adafruit_hid` directory from the unzipped lib/ into your Pi Pico's lib/ directory.
'''
public_key = '''WHAT YOU WANT TO TYPE GOES HERE. E.g. the contents of your PUBLIC ssh key'''
# public_key = '''ssh-ed25519 AJUMBLEOFLETTERSANDNUMBERS... username@host'''
import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
kbd = Keyboard(usb_hid.devices)
kbdl = KeyboardLayoutUS(kbd)
time.sleep(5.0) # Wait 5.0 seconds for everything to get ready
for letter in public_key:
kbd.send(*kbdl.keycodes(letter))
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment