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.
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
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 reportChords, not just releases
Modifiers are keys — hold one with press, layer the chord, then release in any
order:
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| Method | What 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 |
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
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);| Method | What 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 |
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
| Group | Tokens |
|---|---|
| Letters | KEY.A – KEY.Z |
| Digits | KEY.N0 – KEY.N9 |
| Modifiers | KEY.CTRL · KEY.SHIFT · KEY.ALT · KEY.GUI |
| Control | ENTER · ESC · BACKSPACE · TAB · SPACE · CAPSLOCK |
| Navigation | RIGHT · LEFT · DOWN · UP · HOME · END · PAGEUP · PAGEDOWN · INSERT · DEL |
| Function | KEY.F1 – KEY.F12 |
| Punctuation | MINUS · EQUAL · LEFTBRACE · RIGHTBRACE · BACKSLASH · SEMICOLON · APOSTROPHE · GRAVE · COMMA · DOT · SLASH |
| System | PRINTSCREEN · SCROLLLOCK · PAUSE |