githubEdit

Handle User Input

Respond to mouse, touch, keyboard, and controller input

Learn how to respond to user input in Needle Engine—from mouse clicks to touch gestures to VR controllers.

:::tip Cross-Platform by Default Needle Engine's input system works across desktop, mobile, and VR automatically. You don't need to handle different input types separately! :::


Pointer Events (Mouse, Touch, VR)

The easiest way to handle input is using pointer event methods on your components. These work for mouse, touch, and VR controllers automatically.

Basic Click Handler

import { Behaviour, PointerEventData } from "@needle-tools/engine";

export class Clickable extends Behaviour {
    onPointerClick(args: PointerEventData) {
        console.log("Clicked on", this.gameObject.name);
        console.log("World position:", args.point);
    }
}

Add this component to any object and it will respond to clicks!

Available Pointer Events

Method
When Called

onPointerEnter

Mouse/touch starts hovering

onPointerMove

Mouse/touch moves over object

onPointerExit

Mouse/touch stops hovering

onPointerDown

Mouse/touch pressed

onPointerUp

Mouse/touch released

onPointerClick

Mouse/touch clicked (down + up)


Hover Effects

Change object appearance on hover:


Drag and Drop

Track pointer movement while dragging:

:::tip Built-in DragControls For production use, consider the built-in DragControls component which includes:

  • Smooth dragging on surfaces

  • Networking support

  • Throw physics

  • Touch and VR support :::


Keyboard Input

Check keyboard state in update():

Keyboard Methods

Method
Description

getKey(key)

Is key currently held down?

getKeyDown(key)

Was key pressed this frame?

getKeyUp(key)

Was key released this frame?

Key names: Use keyboard event key values like "w", "Space", "ArrowUp", "Escape", etc.


Polling Input State

Check input state continuously in update():


Global Input Events

Subscribe to input events that fire anywhere in the scene (not just on specific objects):

Global Input Events

Event
When Fired

InputEvents.PointerDown

Any pointer down

InputEvents.PointerUp

Any pointer up

InputEvents.PointerMove

Any pointer movement

InputEvents.KeyDown

Any key pressed

InputEvents.KeyUp

Any key released


Browser Events

You can also subscribe to native browser events:

:::warning Platform Differences When using browser events directly, you need to handle platform differences yourself (mouse vs touch vs VR). Needle Engine's input system handles this automatically. :::


Requirements for Pointer Events

For pointer events (onPointerClick, etc.) to work:

  1. Object must be visible - visible = true

  2. GameObject needs geometry - A mesh

  3. Layer not set to "Ignore Raycast"

:::tip For input on invisible objects use Layer Masks By default only visible objects receive input events. You can set the object's layer mask to a different layer, then disable rendering for that layer in the main camera. This way the object can still receive pointer events while being invisible. :::


Touch Gestures

Handle multi-touch gestures:


VR Controller Input

Pointer events work automatically with VR controllers! No extra code needed.

For advanced VR input (button presses, thumbstick), see:


Next Steps

Learn more:

Reference:

Last updated