KeyboardInput
Tracks keyboard state frame-by-frame so you can poll inputs in your loop.
KeyboardInput
Tracks keyboard state frame-by-frame so you can poll inputs in your loop.
Examples
const keyboard = new KeyboardInput();
function tick() {
if (keyboard.isDown("ArrowLeft")) moveLeft();
if (keyboard.wasPressed("Space")) jump();
keyboard.update();
requestAnimationFrame(tick);
}Members
isDown
isDown(code: string): booleanTrue while the key is held down
wasPressed
wasPressed(code: string): booleanTrue only on the first frame the key is pressed
wasReleased
wasReleased(code: string): booleanTrue only on the frame the key is released
update
update(): voidCall once per frame to reset per-frame state
destroy
destroy(): voidTears down DOM listeners to avoid leaks.