The Input Manager

The input manager reads input from four sources: keyboard, mouse, touch screen, and gamepads. It is read inside systems, usually in onUpdate.

In systems extending GameSystem it is available as this.inputManager. See Custom Components and Systems.

import { GameSystem } from "angry-pixel";

export class InputSystem extends GameSystem {
    onUpdate() {
        if (this.inputManager.keyboard.isPressed("Space")) {
            // jump
        }
    }
}

The manager exposes one object per source:

PropertySource
keyboardKeyboard
mouseMouse
touchScreenTouch screen
gamepadsConnected gamepads (array)

Note: Input is disabled in headless mode.

Keyboard

Keys are identified by their KeyboardEvent.code value, such as "Space", "KeyW", or "ArrowUp".

MemberDescription
pressedKeysArray of the codes currently pressed.
isPressed(code)true if the key is pressed.
orPressed(codes)true if any of the keys is pressed.
andPressed(codes)true if all of the keys are pressed.
isPressedReturn(code, ifTrue, ifFalse)Returns one of two values depending on whether the key is pressed.
orPressedReturn(codes, ifTrue, ifFalse)Like isPressedReturn, for any of the keys.
andPressedReturn(codes, ifTrue, ifFalse)Like isPressedReturn, for all of the keys.
const { keyboard } = this.inputManager;

if (keyboard.isPressed("ArrowRight")) {
    // move right
}

if (keyboard.andPressed(["ControlLeft", "KeyC"])) {
    // both keys held together
}

Mouse

MemberTypeDescription
leftButtonPressedbooleanLeft button state.
scrollButtonPressedbooleanScroll (middle) button state.
rightButtonPressedbooleanRight button state.
positionInViewportVector2Cursor position in the viewport.
hasMovedbooleanWhether the mouse moved this frame.
wheelScrollVector2Wheel scroll amount.
const { mouse } = this.inputManager;

if (mouse.leftButtonPressed) {
    const position = mouse.positionInViewport;
    // act at the cursor position
}

Touch screen

MemberTypeDescription
touchingbooleanWhether the screen is being touched.
interactionsTouchInteraction[]Active touch points.

Each TouchInteraction has a positionInViewport (Vector2) and a radius (Vector2, the area of the touch).

const { touchScreen } = this.inputManager;

if (touchScreen.touching) {
    const touch = touchScreen.interactions[0];
    // handle touch at touch.positionInViewport
}

Gamepads

gamepads is an array of connected controllers. Access one by index, and guard against it being absent:

const gamepad = this.inputManager.gamepads[0];

if (gamepad?.bottomFace) {
    // bottom face button (A on Xbox, B on Nintendo, X on PlayStation)
}

const move = gamepad?.leftStickAxes;

A controller exposes the following members:

MemberTypeDescription
connectedbooleanWhether the controller is connected.
idstringController identifier.
indexnumberController index.
bottomFace / rightFace / leftFace / topFacebooleanFace buttons.
dpadUp / dpadDown / dpadLeft / dpadRightbooleanD-pad buttons.
dpadAxesVector2D-pad as an axis.
leftShoulder / rightShoulderbooleanShoulder buttons.
leftTrigger / rightTriggerbooleanTrigger buttons.
back / startbooleanBack and start buttons.
leftStickButton / rightStickButtonbooleanStick press buttons.
leftStickAxes / rightStickAxesVector2Stick positions.
leftStickHorizontal / leftStickVerticalnumberLeft stick axis values.
rightStickHorizontal / rightStickVerticalnumberRight stick axis values.
anyButtonPressedbooleanWhether any button is pressed.
buttonsMap<number, boolean>Raw button states.
axesMap<number, number>Raw axis values.
vibrate(duration, weakMagnitude, strongMagnitude)Triggers vibration, if supported.
gamepad?.vibrate(200, 0.5, 0.5);