InputManager tells your scripts about the keyboard and gamepads. You can react to events as they happen with Register, or check what's held down right now with IsKeyDown and IsGamepadButtonDown. For mouse and touch, use your nodes' Events list or the browser's own listeners on this.dom elements.
| Member | Description |
|---|---|
Register() |
Calls a handler for an input event |
Unregister() |
Removes a handler |
UnregisterAll() |
Removes every handler for an event |
IsKeyDown() |
Whether a key is held |
IsGamepadButtonDown() |
Whether a gamepad button is held |
GamepadButton |
Names for gamepad buttons |
Events
| Event | When | The event has |
|---|---|---|
KeyDown |
A key is pressed, and again as it repeats while held | code, key, repeat, originalEvent |
KeyUp |
A key is released | code, key, originalEvent |
GamepadButtonDown |
A gamepad button is pressed | gamepadIndex, button, value |
GamepadButtonUp |
A gamepad button is released | gamepadIndex, button |
GamepadConnected |
A gamepad is connected, or used for the first time | gamepadIndex, id |
GamepadDisconnected |
A gamepad is disconnected | gamepadIndex |
codeis the physical key, the same on every keyboard layout, such as"Space","KeyW","ArrowUp"or"Escape". Use it for controls.keyis the character the key types on the player's layout, such as"w"or"z". Use it for typing.repeatistruefor the repeats sent while a key is held down.originalEventis the browser'sKeyboardEvent.gamepadIndexsays which gamepad, starting from0.buttonis the button's number (see GamepadButton), andvalueis how far it's pressed, from0to1, for triggers.- Every event also has
name, the event's name.
Register
InputManager.Register(eventName, handler, owner?): void
InputManager.Register(eventName, filter, handler, owner?): void
Calls handler each time the event happens. With a filter, it's only called for that one key or button.
| Parameter | Type | Default | Description |
|---|---|---|---|
eventName |
string |
One of the events above | |
filter |
string | number |
None | A key code like "Space", or a gamepad button number |
handler |
(event) => void |
Called with the event | |
owner |
object |
None | Usually this. See The owner argument |
How it works
- Handlers registered without a filter are called before filtered ones.
- If a handler is
async, the next handler waits for it to finish. - Key events reach your handlers even while the player is typing in a text input, so check for that if it matters.
Example
onStart() {
InputManager.Register("KeyDown", "Space", this.jump, this);
InputManager.Register("GamepadButtonDown", InputManager.GamepadButton.A, this.jump, this);
InputManager.Register("KeyDown", (event) => {
if (event.code === "Escape" && !event.repeat) this.togglePause(); // ignore the repeats while Escape is held
}, this);
},
jump() {
this.dom.player.restart("jump");
},
Unregister
InputManager.Unregister(eventName, handler, owner?): void
InputManager.Unregister(eventName, filter, handler, owner?): void
Removes a handler added with Register. Pass the same filter and the same function, not a bound copy. Handlers registered with an owner are removed for you when the scene exits, and UnregisterScene(owner) removes all of that owner's handlers.
Example
onCutsceneStart() {
InputManager.Unregister("KeyDown", "Space", this.jump, this); // no jumping during the cutscene
},
UnregisterAll
InputManager.UnregisterAll(eventName: string, filter?: string | number): void
Removes every handler for an event, from every scene. Without a filter it removes the handlers registered without one. With a filter, it removes the handlers registered for that key or button.
Example
onGameOver() {
InputManager.UnregisterAll("KeyDown", "Space");
},
IsKeyDown
InputManager.IsKeyDown(code: string): boolean
Returns true while the key with that code is held down. Useful for movement that runs every frame.
Example
x: 0,
onStart() {
nextFrame(() => this.update());
},
update() {
if (InputManager.IsKeyDown("ArrowLeft")) this.x -= 4;
if (InputManager.IsKeyDown("ArrowRight")) this.x += 4;
this.dom.player.style.left = `${this.x}px`;
if (this.dom.player.isConnected) nextFrame(() => this.update()); // stop once the scene has gone
},
IsGamepadButtonDown
InputManager.IsGamepadButtonDown(gamepadIndex: number, button: number): boolean
Returns true while that button is held on that gamepad.
Example
isRunning() {
return InputManager.IsKeyDown("ShiftLeft") || InputManager.IsGamepadButtonDown(0, InputManager.GamepadButton.B);
},
GamepadButton
InputManager.GamepadButton
Names for the button numbers on a standard controller. The face buttons are named by their position on an Xbox controller: A is the bottom one.
| Name | Number | Name | Number |
|---|---|---|---|
A |
0 | Start |
9 |
B |
1 | LeftStick |
10 |
X |
2 | RightStick |
11 |
Y |
3 | DPadUp |
12 |
LeftBumper |
4 | DPadDown |
13 |
RightBumper |
5 | DPadLeft |
14 |
LeftTrigger |
6 | DPadRight |
15 |
RightTrigger |
7 | Home |
16 |
Back |
8 |
Example
onStart() {
InputManager.Register("GamepadButtonDown", InputManager.GamepadButton.Start, this.togglePause, this);
},