# Keyboard Emulator Using a Pi Pico and CircuitPython # Additional Libraries: adafruit_hid # https://github.com/adafruit/Adafruit_CircuitPython_HID # Original code: 2018 Kattni Rembor for Adafruit Industries (MIT License) # Updated version: 2026 Steve McL for Iffy Books (wtfpl) # https://iffybooks.net/diy-macro-keyboard 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 time.sleep(1) # Sleep to avoid race conditions # The keyboard object keyboard = Keyboard(usb_hid.devices) keyboard_layout = KeyboardLayoutUS(keyboard) # US keyboard layout gpio_pin_dict = {0: [board.GP0, Keycode.FIVE], 1: [board.GP1, Keycode.F], 2: [board.GP2, Keycode.B], 3: [board.GP3, Keycode.D], 4: [board.GP4, [Keycode.SHIFT,Keycode.LEFT_ARROW]], 5: [board.GP5, [Keycode.SHIFT,Keycode.RIGHT_ARROW]], 6: [board.GP6, Keycode.F1], 7: [board.GP7, Keycode.F2], 8: [board.GP8, Keycode.H], 9: [board.GP9, Keycode.G], 10: [board.GP10, Keycode.ZERO], 11: [board.GP11, Keycode.N], 12: [board.GP12, Keycode.SEMICOLON], 13: [board.GP13, Keycode.L], 14: [board.GP14, Keycode.F5], 15: [board.GP15, Keycode.F6], 16: [board.GP16, Keycode.F8], 17: [board.GP17, Keycode.F7], 18: [board.GP18, ""], 19: [board.GP19, Keycode.P], 20: [board.GP20, Keycode.DOWN_ARROW], 21: [board.GP21, Keycode.UP_ARROW], 22: [board.GP22, Keycode.SIX], 26: [board.GP26, Keycode.ONE], 27: [board.GP27, Keycode.F4], 28: [board.GP28, Keycode.F3] } for pin_number in gpio_pin_dict: pin = gpio_pin_dict[pin_number][0] key_pin = digitalio.DigitalInOut(pin) key_pin.direction = digitalio.Direction.INPUT key_pin.pull = digitalio.Pull.UP gpio_pin_dict[pin_number].append(key_pin) led = digitalio.DigitalInOut(board.GP25) led.direction = digitalio.Direction.OUTPUT led.value = False # Infinite loop checking whether a key is pressed while True: for pin_number in gpio_pin_dict: key_pin = gpio_pin_dict[pin_number][2] if not key_pin.value: keycode = gpio_pin_dict[pin_number][1] try: keyboard.press(keycode) except: keyboard.press(keycode[0],keycode[1]) time.sleep(0.2) keyboard.release_all()