Input

Input

Keyboard and mouse input management with raycasting support.

InputManager tracks keyboard and mouse 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:

ParameterTypeDescription
exceptObject3D[]Objects to exclude from raycasting
onlyObject3D[]Only raycast against these objects
// Raycast only against the terrain
const pos = input.getPointerWorldPosition(undefined, [terrain.instance])