This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhid-keys-simple.py
69 lines (56 loc) · 1.96 KB
/
hid-keys-simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
#
# SPDX-License-Identifier: MIT
# A simple example of how to set up a keymap and HID keyboard on Keybow 2040.
# You'll need to connect Keybow 2040 to a computer, as you would with a regular
# USB keyboard.
# Drop the `keybow2040.py` file and `keybow_hardware` folder
# into your `lib` folder on your `CIRCUITPY` drive.
# NOTE! Requires the adafruit_hid CircuitPython library also!
from keybow2040 import Keybow2040
from keybow_hardware.pim56x import PIM56X as Hardware # for Keybow 2040
#from keybow_hardware.pim551 import PIM551 as Hardware # for Pico RGB Keypad Base
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# Set up Keybow
keybow = Keybow2040(Hardware())
keys = keybow.keys
# Set up the keyboard and layout
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
# A map of keycodes that will be mapped sequentially to each of the keys, 0-15
keymap = [Keycode.ZERO,
Keycode.ONE,
Keycode.TWO,
Keycode.THREE,
Keycode.FOUR,
Keycode.FIVE,
Keycode.SIX,
Keycode.SEVEN,
Keycode.EIGHT,
Keycode.NINE,
Keycode.A,
Keycode.B,
Keycode.C,
Keycode.D,
Keycode.E,
Keycode.F]
# The colour to set the keys when pressed, yellow.
rgb = (255, 255, 0)
# Attach handler functions to all of the keys
for key in keys:
# A press handler that sends the keycode and turns on the LED
@keybow.on_press(key)
def press_handler(key):
keycode = keymap[key.number]
keyboard.send(keycode)
key.set_led(*rgb)
# A release handler that turns off the LED
@keybow.on_release(key)
def release_handler(key):
key.led_off()
while True:
# Always remember to call keybow.update()!
keybow.update()