Skip to content

Input

Updated

Input is how a script learns what the player is doing with their devices: a key held down, the mouse moving, a finger dragging across a phone, a thumbstick pushed in a headset, a phone tilted, a voice speaking. The Input global gives a Behavior script every one of these devices in the same way on desktop, mobile and XR, so you can build any control scheme you like.

To let players click, grab or drag objects without reading devices yourself, use the interaction components instead: see Interaction.

  • Polling. Reading the state of a device when you need it, usually every frame in update: “is the W key held right now?”. Every device on Input is polled this way.

  • Frame snapshot. Each device takes one reading per frame, the first time any script asks. Every script then sees the same keys, buttons and poses during that frame, whatever order the scripts run in.

  • Pressed and released. isKeyPressed (and its mouse and XR equivalents) is true on every frame a key is held: use it for continuous actions such as walking. isKeyReleased is true on exactly one frame, after the key goes up: use it for one-off actions such as jumping or opening a menu. A tap too quick to span a frame still reads as pressed for one frame, so nothing is missed.

  • Screen point. A position on the screen in CSS pixels, measured from the top-left corner of the page. The mouse, touches, raycasts from the screen and CoordinateSpace all use this same space.

  • Capture device. The microphone, the device camera, screen sharing and location. They start only when a script asks, ask the browser’s permission the first time, and stop once no script uses them.

Every frame, MIRA runs the components’ own work, then each Behavior’s update, in execution order. Input fits into that loop like this:

  1. The first query reads the device. The first time a script asks Input.keyboard (or any other device) something during a frame, the device takes its snapshot for that frame. Later questions in the same frame read the same snapshot.
  2. Scripts poll in update. A script checks keys, buttons, sticks and poses and acts on them. Because the snapshot is shared, two scripts that both check isKeyReleased(Key.Space) in the same frame both see true.
  3. Capture devices are shared. If two scripts use the microphone, it opens once. When a script stops it, or the script is destroyed, its use is released; the device turns off when nobody uses it any more.

A few rules follow from this design:

  • Keys typed into a text field are not seen by Input.keyboard, so a player typing a chat message does not also walk forward.
  • Leaving the window releases every held key and button.

This walkthrough moves a box with the arrow keys or WASD, and makes it hop when the player presses Space.

  1. Add a Primitive Mesh with a box shape and name it Player Box.

  2. Add a Behavior entity anywhere in the scene with this script:

    MoveBox.jx
    #pragma lifecycle(startup, update)
    const SPEED = 3; // metres a second
    let box;
    function startup() {
    box = findEntity('Player Box');
    }
    function update(dt) {
    const keys = Input.keyboard;
    if (keys.isKeyPressed(Key.ArrowLeft) || keys.isKeyPressed(Key.A)) box.position.x -= SPEED * dt;
    if (keys.isKeyPressed(Key.ArrowRight) || keys.isKeyPressed(Key.D)) box.position.x += SPEED * dt;
    if (keys.isKeyPressed(Key.ArrowUp) || keys.isKeyPressed(Key.W)) box.position.z -= SPEED * dt;
    if (keys.isKeyPressed(Key.ArrowDown) || keys.isKeyPressed(Key.S)) box.position.z += SPEED * dt;
    // Released is true on one frame only, so each press hops once.
    if (keys.isKeyReleased(Key.Space)) box.position.y += 0.5;
    }
  3. Enter play, click the view so it has the keyboard focus, and use the arrow keys or WASD.

  • startup looks the box up once. Looking it up in update would repeat the search every frame.
  • isKeyPressed keeps moving the box for as long as a key is held; multiplying by dt (the seconds since the last frame) keeps the speed the same at any frame rate.
  • isKeyReleased(Key.Space) is true for a single frame, so one press makes one hop.
If you want… Use… Read
Keys, mouse buttons, the wheel or touches Input.keyboard, Input.mouse, Input.touch Keyboard, mouse and touch
XR controller buttons, sticks, hand joints and gestures Input.xr.controller(...) XR controllers and hands
Tilt, device orientation or GPS position Input.accelerometer, Input.gyroscope, Input.location Motion sensors and location
Voice level, recordings, a webcam picture or a shared screen Input.microphone, Input.camera, Input.screen Microphone, camera and screen
To adapt to the platform, vibrate a controller or light the torch Device, Haptics, Torch, WebBrowser Device and haptics
On-screen sticks and buttons for phones and tablets Screen Gamepad Screen Gamepad
Click, grab or drag objects Interaction components Interaction
GlobalWhat it is
InputEvery input device: keyboard, mouse, touch, XR, accelerometer, gyroscope, microphone, device camera, location.
KeyDefines the values for standard keys
MouseButtonThe MouseButton enum provides constants representing the different mouse buttons.
XRButton
DevicePlatform facts for scripts that adapt to the device they run on.
HapticsVibration on whatever the player has: XR controllers in an XR session, a gamepad's rumble when one is connected, the phone otherwise.
TorchThe phone's torch, by its back camera: await Torch.on(), off(), toggle(), isOn().
WebBrowserRead the browser location and open new tabs.
ScreenGamepadComponentAn on-screen gamepad for the player looking through this camera: a left stick, a right stick and the A, B, X and Y buttons, shown over the view while this camera renders, in play, never in VR. It changes no component: it feeds the shared gamepad as a second pad, so everything that reads a gamepad reacts — a Character Controller walks, runs and jumps, and the camera's controller looks around — and a real gamepad works beside it. Its buttons also send press and release on the entity's Signal Emitter.
ComponentTypeWhat it does
Screen Gamepadcomp-screen-gamepadAn on-screen gamepad with two sticks and four buttons, for touch screens.
Topic Description
Keyboard, mouse and touch Poll keys, mouse buttons, the wheel and touches; the Key and MouseButton constants.
XR controllers and hands Buttons, triggers, thumbsticks, poses, hand joints, finger curls and gestures in a headset.
Motion sensors and location Accelerometer, gyroscope and GPS, with their permission prompts.
Microphone, camera and screen Voice level and recordings, the device camera as a texture or photo, screen sharing.
Device and haptics Platform checks, controller and phone vibration, the phone torch and the page URL.
Screen Gamepad Touch sticks and buttons drawn over the view, feeding the shared gamepad.