Input
InputManager tracks keyboard, mouse and gamepad state and provides raycasting utilities to convert screen coordinates into world positions.
// Accessed via game.input
const input = game.input
Keyboard
Keys are tracked by their KeyboardEvent.code value.
// Check if a key is currently held down
if (input.keysPressed.has('KeyW')) {
// W is pressed
}
if (input.keysPressed.has('Space')) {
// Space is pressed
}
Keyboard events
input.on('keydown', (event: KeyboardEvent) => {
console.log('pressed:', event.code)
})
input.on('keyup', (event: KeyboardEvent) => {
console.log('released:', event.code)
})
Mouse
Mouse button state is tracked by button index (0 = left, 1 = wheel, 2 = right).
if (input.mousePressed.has(0)) {
// Left mouse button is held
}
Cursor position
The normalized cursor position in the range [-1, 1] on both axes:
input.cursor // THREE.Vector2, updated every mousemove
World position raycasting
Convert the cursor position into a world-space position using getPointerWorldPosition:
const worldPos = input.getPointerWorldPosition()
// Returns a THREE.Vector3 | null
Parameters:
| Parameter | Type | Description |
|---|---|---|
except | Object3D[] | Objects to exclude from raycasting |
only | Object3D[] | Only raycast against these objects |
// Raycast only against the terrain
const pos = input.getPointerWorldPosition(undefined, [terrain.instance])
Gamepads
Xbox, PlayStation, Nintendo and generic controllers are available through input.gamepads, a GamepadManager. It is polled once per game update, before the world updates.
Browsers only expose a controller after a button on it was pressed while the page has focus.
import { GamepadButton } from '@mavonengine/core/GamepadManager'
const pad = input.gamepads.primary // first connected controller, or undefined
if (pad) {
// Sticks after the deadzone, Y is positive when pushed up
move.set(pad.leftStick.x, 0, -pad.leftStick.y)
if (pad.justPressed(GamepadButton.South)) // A on Xbox, Cross on PlayStation
jump()
const throttle = pad.value(GamepadButton.RightTrigger) // 0 - 1
}
// Any connected controller
input.gamepads.isPressed(GamepadButton.Start)
input.gamepads.justPressed(GamepadButton.A)
Buttons
GamepadButton follows the W3C standard mapping. Use the layout agnostic names (South, East, West, North, LeftBumper, RightTrigger, DPadUp, …) or the Xbox (A, B, LB, RT, View, Menu) and PlayStation (Cross, Circle, L1, R2, Share, Options) aliases.
GamepadState
| Member | Description |
|---|---|
type | 'xbox', 'playstation', 'nintendo' or 'generic', detected from the id |
leftStick / rightStick | Vector2 after the radial deadzone |
axes | Raw axes as reported by the browser |
isPressed(button) | Button is held |
justPressed(button) / justReleased(button) | Button changed since the previous update |
value(button) | Analog value, 0 - 1 (triggers) |
buttonLabel(button) | Label for the controller family, e.g. "Cross" or "A" |
Settings
input.gamepads.stickDeadzone = 0.15 // stick input below this is ignored
input.gamepads.buttonThreshold = 0.5 // analog buttons count as pressed above this
Events
input.gamepads.on('connected', (pad) => console.log(`${pad.type} connected`))
input.gamepads.on('disconnected', (pad) => {})
input.gamepads.on('buttondown', ({ gamepad, button }) => console.log(gamepad.buttonLabel(button)))
input.gamepads.on('buttonup', ({ gamepad, button }) => {})
Rumble
await input.gamepads.vibrate(pad.index, { duration: 200, strongMagnitude: 1, weakMagnitude: 0.5 })
// Resolves to false when the controller or browser doesn't support it
The level editor's status bar shows connected controllers with a live debug view.