keydown
Keyboard event stream that emits only keydown events. Shares underlying listeners per EventTarget.
Basic Usage
import { keydown } from "cereb";
keydown(window).on((signal) => {
console.log("Key pressed:", signal.value.code);
Signature
function keydown(target: EventTarget, options?: KeydownOptions): Stream<KeyboardSignal>
Options
| Option | Type | Default | Description |
|---|
code | KeyCode | KeyCode[] | - | Filter by physical key code(s) |
modifiers | ModifierKey[] | - | Filter by modifier keys (OR logic) |
preventDefault | boolean | true | Call preventDefault on matching events |
allowRepeat | boolean | false | Allow repeated keydown events |
Signal Value
| Property | Type | Description |
|---|
phase | "down" | Always “down” for keydown |
key | string | Logical key value (e.g., ”+”, “a”, “Meta”) |
code | string | Physical key code (e.g., “Equal”, “KeyA”) |
repeat | boolean | True if repeated from holding key |
altKey | boolean | Alt key pressed |
ctrlKey | boolean | Ctrl key pressed |
metaKey | boolean | Meta (Cmd/Win) key pressed |
shiftKey | boolean | Shift key pressed |
originalEvent | KeyboardEvent | Original DOM event |
Examples
Zoom with +/- Keys
keydown(window, { code: ["Equal", "Minus"] }).on((signal) => {
const zoomIn = signal.value.code === "Equal";
setScale(scale * (zoomIn ? 1.2 : 0.8));
Undo/Redo Shortcut
// Cmd+Z / Ctrl+Z for undo
modifiers: ["meta", "ctrl"]
// Cmd+Shift+Z / Ctrl+Shift+Z for redo
modifiers: ["meta", "ctrl"]
if (signal.value.shiftKey) {
Allow Key Repeat
// Continuous scrolling while holding arrow key
code: ["ArrowUp", "ArrowDown"],
const direction = signal.value.code === "ArrowUp" ? -1 : 1;