Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created March 15, 2020 21:39
Show Gist options
  • Save FoamyGuy/d3dc4750a751538aed590a50e652c76a to your computer and use it in GitHub Desktop.
Save FoamyGuy/d3dc4750a751538aed590a50e652c76a to your computer and use it in GitHub Desktop.
import board
from digitalio import DigitalInOut, Direction, Pull
import adafruit_dotstar as dotstar
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from miniKbdButtons import MiniKbdButtons
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
dot[0] = (0, 0, 0)
kbd = Keyboard(usb_hid.devices)
kbdLayout = KeyboardLayoutUS(kbd)
def shortcut_select_all():
kbd.press(Keycode.LEFT_CONTROL)
kbd.press(Keycode.A)
kbd.release_all()
# Customize these keycodes
# https://circuitpython.readthedocs.io/projects/hid/en/latest/api.html#adafruit-hid-keycode-keycode
buttonIDtoKeycode = {
1: shortcut_select_all,
2: Keycode.TWO,
3: Keycode.THREE,
4: Keycode.FOUR,
5: Keycode.FIVE,
6: Keycode.SIX}
def buttonDownCallback(buttonID, othersDown):
if not callable(buttonIDtoKeycode[buttonID]):
# normal keycode
kbd.press(buttonIDtoKeycode[buttonID])
dot[0] = (255, 0, 0) # Red LED
print("Button _down_", buttonID, othersDown)
else:
# shortcut function
buttonIDtoKeycode[buttonID]()
def buttonUpCallback(buttonID):
if not callable(buttonIDtoKeycode[buttonID]):
kbd.release(buttonIDtoKeycode[buttonID])
dot[0] = (0, 0, 0)
print("Button ^UPUP^", buttonID)
ButtonMatrix = MiniKbdButtons(
keyDownCallback=buttonDownCallback,
keyUpCallback=buttonUpCallback)
# Main Loop
while True:
ButtonMatrix.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment