Last active
September 5, 2025 05:46
-
-
Save markwatson/b6401024d52b4e00f39876cb8d3b197d to your computer and use it in GitHub Desktop.
Circuit Python Keyboard Driver for Adafruit KB2040 & NeoKey 1x4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries | |
# | |
# SPDX-License-Identifier: MIT | |
# Modified by Mark Watson, 2025 (MIT license) | |
"""CircuitPython Essentials HID Keyboard example""" | |
import time | |
import board | |
import usb_hid | |
# Docs: https://docs.circuitpython.org/projects/hid/en/latest/api.html | |
# Library: https://learn.adafruit.com/circuitpython-essentials/circuitpython-hid-keyboard-and-mouse | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
# Library: https://learn.adafruit.com/neokey-1x4-qt-i2c/python-circuitpython | |
from adafruit_neokey.neokey1x4 import NeoKey1x4 | |
# use default I2C bus | |
i2c_bus = board.I2C() | |
neokey = NeoKey1x4(i2c_bus, addr=0x30) | |
# The keyboard object! | |
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems | |
keyboard = Keyboard(usb_hid.devices) | |
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) | |
# Keymap (note the NeoKey1x4 has 4 keys | |
keymap = [ | |
# Send shortcuts unlikely to be used, then remap later | |
[Keycode.COMMAND, Keycode.CONTROL, Keycode.OPTION, Keycode.F1], | |
[Keycode.COMMAND, Keycode.CONTROL, Keycode.OPTION, Keycode.F2], | |
[Keycode.COMMAND, Keycode.CONTROL, Keycode.OPTION, Keycode.F3], | |
[Keycode.COMMAND, Keycode.CONTROL, Keycode.OPTION, Keycode.F4], | |
] | |
keys = len(keymap) | |
while True: | |
for i in range(len(keymap)): | |
if neokey[i]: | |
keyboard.press(*keymap[i]) | |
else: | |
keyboard.release(*keymap[i]) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment