You can customize the keyboard's RGB LEDs using TypeScript and DeviceScript. This example demonstrates how to light up a key in red when it is pressed and automatically turn it off after 400ms.
Key components used:
KeyboardClient: To listen for key press events.startLed: To initialize the LED strip (WS2812B_GRB on GPIO 7).hid2idx: A utility mapping to translate HID key codes to LED indices.
import { KeyboardClient, LedStripLightType, LedVariant, gpio } from "@devicescript/core";
import { startLed } from "@devicescript/drivers";
import { hid2idx } from "./utils/ledmap";
// Initialize keyboard LEDs
const led = await startLed({
length: 13*5,
columns: 5,
variant: LedVariant.Strip,
hwConfig: { type: LedStripLightType.WS2812B_GRB, pin: gpio(7) },
})
await led.showAll(0) // Turn all LEDs off
// Initialize keyboard client
const kb = new KeyboardClient()
// Get the pixel buffer
const pixels = await led.buffer()
let lastLightNum = 0
let lastClearLight: number = null
// Listen for key down events
kb.down.subscribe(async (key) => {
if (hid2idx[key]) {
if(lastClearLight && lastLightNum === hid2idx[key]){
// Clear timeout if key is held down
clearTimeout(lastClearLight)
} else {
let lastLight = hid2idx[key]
// Set pressed key color to red
pixels.setAt(hid2idx[key], 0xff0000)
await led.show();
}
// Set auto-off timer for 400ms
lastClearLight = setTimeout(async() => {
pixels.setAt(hid2idx[key], 0)
await led.show();
lastClearLight = null
}, 400)
}
})