Text Editor
Adafruit recommends using the Mu editor for using your CircuitPython code with the Pico. You can get more info in this guide.
Alternatively, you can use any text editor that saves files.
# SPDX-FileCopyrightText: 2021 john park for Adafruit Industries # SPDX-License-Identifier: MIT # Pico Four Key USB HID Keypad import board from digitalio import DigitalInOut, Pull from adafruit_debouncer import Debouncer import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kpd = Keyboard(usb_hid.devices) # Choose the correct modifier key for Windows or Mac. # Comment one line and uncomment the other. # MODIFIER = Keycode.CONTROL # For Windows MODIFIER = Keycode.COMMAND # For Mac # define buttons NUM_KEYS = 4 PINS = ( board.GP0, board.GP1, board.GP2, board.GP3, ) KEYMAP = ( ("Select all", [MODIFIER, Keycode.A]), ("Cut", [MODIFIER, Keycode.X]), ("Copy", [MODIFIER, Keycode.C]), ("Paste", [MODIFIER, Keycode.V]), ) keys = [] for pin in PINS: dio = DigitalInOut(pin) dio.pull = Pull.UP keys.append(Debouncer(dio)) print("\nWelcome to keypad") print("keymap:") for k in range(NUM_KEYS): print("\t", (KEYMAP[k][0])) while True: for i in range(NUM_KEYS): keys[i].update() if keys[i].fell: print(KEYMAP[i][0]) kpd.send(*KEYMAP[i][1])
Testing
Even before we've wired up the mechanical keyswitches, you can test the code by shorting each of the four GPIO pins used to ground. This is the same as pressing a key. Just be careful not to short power to ground!
Use a text editor with some text in it to watch as your keys select all, cut, copy, and paste!
HID Keyboard Basics
This guide page has a great intro to CircuitPython HID Keyboard.
For even more details, check out the documentation at https://circuitpython.readthedocs.io/projects/hid/en/latest/ which includes all of the keycodes and media codes you can use.
By importing the adafruit_hid library into the program, you can make calls to send keyboard keys and media keys.
Customization
You can have your keypad type other keys or key combos if you like. This is the section of code to adjust:
KEYMAP = ( ("Select all", [MODIFIER, Keycode.A]), ("Cut", [MODIFIER, Keycode.X]), ("Copy", [MODIFIER, Keycode.C]), ("Paste", [MODIFIER, Keycode.V]), )
The string is only there for debugging/testing purposes. The section inside the brackets is where you can put your own keys and combos. Just remember to add a comma between each key in a combo.
Text editor powered by tinymce.