USB HID — Keyboard & Mouse

The board becomes a USB input device: Keyboard and Mouse speak the boot-protocol HID reports a host already understands, so no drivers are needed — plug in and it types and moves. Every verb here is hardware-tested on a real board; the examples are the tests.

class

Keyboard

A boot-protocol keyboard: an 8-byte report — one modifier byte plus six concurrent key slots. press/release submit one report each; modifiers OR into the modifier byte, keys fill slots in press order.

Example first

keyboard.ts
import { Keyboard, KEY } from '@typecad/hal';

const kb = new Keyboard();
kb.begin();                 // register the descriptor, start the USB stack

kb.press(KEY.CTRL);
kb.press(KEY.SHIFT);
kb.press(KEY.ESC);          // Ctrl+Shift+Esc
kb.releaseAll();            // clean slate — zeroes the whole report

Chords, not just releases

Modifiers are keys — hold one with press, layer the chord, then release in any order:

chord.ts
kb.press(KEY.GUI);          // hold the modifier...
kb.press(KEY.D);            // ...while pressing D
kb.release(KEY.D);
kb.release(KEY.GUI);        // modifiers release like any other key
MethodWhat it does
begin()Registers the boot-keyboard descriptor and starts the USB device stack — once, before any press
press(key)Modifier → OR into the modifier byte; key → first free slot
release(key)Release a key or modifier
releaseAll()Zero the whole report — every slot and modifier
class

Mouse

A boot-protocol mouse: relative movement and up to three buttons. x/y clamp to ±127 per report — int8 is the wire format, so an oversized move saturates instead of producing a malformed report. The wheel rolls in y.

Example first

mouse.ts
import { Mouse, MOUSE } from '@typecad/hal';

const mouse = new Mouse();
mouse.begin();

mouse.move(10, -4);         // relative — right 10, up 4
mouse.move(0, 0, 1);        // third argument: one wheel step
mouse.click(MOUSE.LEFT);    // press + release as two reports

mouse.press(MOUSE.LEFT | MOUSE.RIGHT);   // buttons OR together
mouse.release(MOUSE.LEFT | MOUSE.RIGHT);
MethodWhat it does
begin()Registers the boot-mouse descriptor and starts the USB device stack
move(dx, dy, wheel?)Relative motion, clamped to ±127 per report; wheel defaults to 0
press(button) / release(button)Hold and let go — MOUSE.LEFT · RIGHT · MIDDLE, OR-able
click(button)Press and release as two reports
token tables

KEY / MOUSE tokens

Key and button arguments are tokens, not magic numbers — each maps name-for-name onto Zephyr's HID_KEY_* usage codes, and the transpiler splices them into the generated reports.

Example first

GroupTokens
LettersKEY.A – KEY.Z
DigitsKEY.N0 – KEY.N9
ModifiersKEY.CTRL · KEY.SHIFT · KEY.ALT · KEY.GUI
ControlENTER · ESC · BACKSPACE · TAB · SPACE · CAPSLOCK
NavigationRIGHT · LEFT · DOWN · UP · HOME · END · PAGEUP · PAGEDOWN · INSERT · DEL
FunctionKEY.F1 – KEY.F12
PunctuationMINUS · EQUAL · LEFTBRACE · RIGHTBRACE · BACKSLASH · SEMICOLON · APOSTROPHE · GRAVE · COMMA · DOT · SLASH
SystemPRINTSCREEN · SCROLLLOCK · PAUSE
© 2026 typeCAD — code-defined electronics github