GamepadInput
Gamepad-style unified input that reads from keyboard. Virtual peripherals (dpad, joystick, buttons) can be polled separately and combined with this in the game loop.
GamepadInput
Gamepad-style unified input that reads from keyboard. Virtual peripherals (dpad, joystick, buttons) can be polled separately and combined with this in the game loop.
Examples
const input = new GamepadInput({
up: { keys: ["ArrowUp", "KeyW"] },
down: { keys: ["ArrowDown", "KeyS"] },
left: { keys: ["ArrowLeft", "KeyA"] },
right: { keys: ["ArrowRight", "KeyD"] },
jump: { keys: ["Space"] },
shoot: { keys: ["KeyZ", "KeyJ"] },
});
function gameLoop() {
input.update();
if (input.isDown("left")) player.moveLeft();
if (input.wasPressed("jump")) player.jump();
requestAnimationFrame(gameLoop);
}Members
update
update(): voidCall once per frame before reading input
isDown
isDown(action: string): booleanTrue while any mapped key for this action is held
wasPressed
wasPressed(action: string): booleanTrue only on the first frame any mapped key for this action is pressed
wasReleased
wasReleased(action: string): booleanTrue only on the frame any mapped key for this action is released
getAxis
getAxis(negativeAction: string, positiveAction: string): numberGet a horizontal axis value: -1 (left), 0, or 1 (right)
destroy
destroy(): voidRemoves keyboard listeners.