게임 만들기

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): boolean

True while the key is held down

wasPressed

wasPressed(code: string): boolean

True only on the first frame the key is pressed

wasReleased

wasReleased(code: string): boolean

True only on the frame the key is released

update

update(): void

Call once per frame to reset per-frame state

destroy

destroy(): void

Tears down DOM listeners to avoid leaks.