Text Editor

Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.

Alternatively, you can use any text editor that saves simple text files.

Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the code.py file, along with a folder full of key configuration files. To get everything you need, click on the Download Project Bundle link below, and uncompress the .zip file.

Drag the contents of the uncompressed bundle directory onto your Feather board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
# SPDX-License-Identifier: MIT
# NUMPAD 4000! Made with snap-apart NeoKey PCB and Feather RP2040.

import board
import keypad
import neopixel
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

COLUMNS = 5
ROWS = 5

BLUE = 0x000510
WHITE = 0x303030
RED = 0xFF0000


board_pix = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
board_pix[0] = BLUE

key_pixels = neopixel.NeoPixel(board.D5, 30, brightness=0.1)
key_pixels.fill(WHITE)

keys = keypad.KeyMatrix(
    row_pins=(board.D4, board.A3, board.A2, board.A1, board.A0),
    column_pins=(board.D13, board.D12, board.D11, board.D10, board.D9),
    columns_to_anodes=False,
)

kbd = Keyboard(usb_hid.devices)

keycode_LUT = [
             0, 1, 2, 3, 4,
             5, 6, 7, 8,
             10, 11, 12, 13, 14,
             15, 16, 17, 18,
             20, 21, 23, 24
]

pixel_LUT = [
             0, 1, 2, 3, 4,
             8, 7, 6, 5,
             10, 11, 12, 13, 14,
             18, 17, 16, 15,
             20, 21, 23, 24
]
# create a keycode dictionary including modifier state and keycodes
keymap = {
            (0): (0, Keycode.KEYPAD_NUMLOCK),
            (1): (0, Keycode.BACKSPACE),
            (2): (0, Keycode.FORWARD_SLASH),
            (3): (0, Keycode.KEYPAD_ASTERISK),
            (4): (0, Keycode.KEYPAD_MINUS),

            (5): (0, Keycode.PAGE_UP),
            (6): (0, Keycode.KEYPAD_SEVEN),
            (7): (0, Keycode.KEYPAD_EIGHT),
            (8): (0, Keycode.KEYPAD_NINE),

            (9): (0, Keycode.PAGE_DOWN),
            (10): (0, Keycode.KEYPAD_FOUR),
            (11): (0, Keycode.KEYPAD_FIVE),
            (12): (0, Keycode.KEYPAD_SIX),
            (13): (0, Keycode.KEYPAD_PLUS),

            (14): (1, Keycode.SHIFT),
            (15): (0, Keycode.KEYPAD_ONE),
            (16): (0, Keycode.KEYPAD_TWO),
            (17): (0, Keycode.KEYPAD_THREE),

            (18): (2, Keycode.CONTROL),
            (19): (0, Keycode.KEYPAD_ZERO),
            (20): (0, Keycode.KEYPAD_PERIOD),
            (21): (0, Keycode.KEYPAD_EQUALS)  # KEYPAD_ENTER on non-mac
}

shift_mod = False
ctrl_mod = False


while True:

    key_event = keys.events.get()
    if key_event:
        if key_event.pressed:
            if keymap[keycode_LUT.index(key_event.key_number)][0] == 1:
                shift_mod = True
            elif keymap[keycode_LUT.index(key_event.key_number)][0] == 2:
                ctrl_mod = True
            if shift_mod is False and ctrl_mod is False:
                kbd.press(keymap[keycode_LUT.index(key_event.key_number)][1])
                print(keymap[keycode_LUT.index(key_event.key_number)][1])
                key_pixels[pixel_LUT.index(key_event.key_number)] = RED
            elif shift_mod is True and ctrl_mod is False:
                kbd.press(Keycode.SHIFT, keymap[keycode_LUT.index(key_event.key_number)][1])
                print(keymap[keycode_LUT.index(key_event.key_number)][1])
                key_pixels[pixel_LUT.index(key_event.key_number)] = RED
            elif shift_mod is False and ctrl_mod is True:
                kbd.press(Keycode.CONTROL, keymap[keycode_LUT.index(key_event.key_number)][1])
                print(keymap[keycode_LUT.index(key_event.key_number)][1])
                key_pixels[pixel_LUT.index(key_event.key_number)] = RED
            elif shift_mod is True and ctrl_mod is True:
                kbd.press(
                          Keycode.SHIFT,
                          Keycode.CONTROL,
                          keymap[keycode_LUT.index(key_event.key_number)][1]
                          )
                print(keymap[keycode_LUT.index(key_event.key_number)][1])
                key_pixels[pixel_LUT.index(key_event.key_number)] = RED
            board_pix[0] = WHITE

        if key_event.released:
            if keymap[keycode_LUT.index(key_event.key_number)][0] == 1:  # un-shift
                shift_mod = False
            elif keymap[keycode_LUT.index(key_event.key_number)][0] == 2:  # un-ctrl
                ctrl_mod = False

            kbd.release(keymap[keycode_LUT.index(key_event.key_number)][1])
            key_pixels[pixel_LUT.index(key_event.key_number)] = WHITE
            board_pix[0] = BLUE

Use the Numpad 4000

Once the libraries and code are installed, the Numpad 4000 will work as a USB HID keyboard device. You can try it out right away by plugging the keyboard into your computer via a known good USB cable and then typing in some numbers and symbols.

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.

If you want to customize the keys, to send different keycodes, this dictionary contains all of the mappings:

keymap = {
            (0): (0, Keycode.KEYPAD_NUMLOCK),
            (1): (0, Keycode.BACKSPACE),
            (2): (0, Keycode.FORWARD_SLASH),
            (3): (0, Keycode.KEYPAD_ASTERISK),
            (4): (0, Keycode.KEYPAD_MINUS),

            (5): (0, Keycode.PAGE_UP),
            (6): (0, Keycode.KEYPAD_SEVEN),
            (7): (0, Keycode.KEYPAD_EIGHT),
            (8): (0, Keycode.KEYPAD_NINE),

            (9): (0, Keycode.PAGE_DOWN),
            (10): (0, Keycode.KEYPAD_FOUR),
            (11): (0, Keycode.KEYPAD_FIVE),
            (12): (0, Keycode.KEYPAD_SIX),
            (13): (0, Keycode.KEYPAD_PLUS),

            (14): (1, Keycode.SHIFT),
            (15): (0, Keycode.KEYPAD_ONE),
            (16): (0, Keycode.KEYPAD_TWO),
            (17): (0, Keycode.KEYPAD_THREE),

            (18): (2, Keycode.CONTROL),
            (19): (0, Keycode.KEYPAD_ZERO),
            (20): (0, Keycode.KEYPAD_PERIOD),
            (21): (0, Keycode.KEYPAD_EQUALS)  # KEYPAD_ENTER on non-mac
}

This guide was first published on Aug 17, 2021. It was last updated on 2021-08-17 21:16:49 -0400.

This page (Code and use the Numpad 4000) was last updated on Sep 18, 2021.

Text editor powered by tinymce.