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.
Key concepts
Section titled “Key concepts”-
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 onInputis 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) istrueon every frame a key is held: use it for continuous actions such as walking.isKeyReleasedistrueon 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
CoordinateSpaceall 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.
How it works
Section titled “How it works”Every frame, MIRA runs the components’ own work, then each Behavior’s update, in execution order. Input fits into that loop like this:
- 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. - 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 checkisKeyReleased(Key.Space)in the same frame both seetrue. - 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.
Your first input script
Section titled “Your first input script”This walkthrough moves a box with the arrow keys or WASD, and makes it hop when the player presses Space.
-
Add a Primitive Mesh with a box shape and name it
Player Box. -
Add a Behavior entity anywhere in the scene with this script:
MoveBox.jx #pragma lifecycle(startup, update)const SPEED = 3; // metres a secondlet 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;} -
Enter play, click the view so it has the keyboard focus, and use the arrow keys or WASD.
startuplooks the box up once. Looking it up inupdatewould repeat the search every frame.isKeyPressedkeeps moving the box for as long as a key is held; multiplying bydt(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.
Your first input script: the arrow keys or WASD move a box, multiplied by dt so the speed is the same at any frame rate, and Space makes it hop.
Click the view so it has the keyboard focus, then use the arrow keys or WASD, and Space to hop.
Choosing
Section titled “Choosing”| If you want… | Use… | Read |
|---|---|---|
| Keys, mouse buttons, the wheel or touches | Input., Input.mouse, Input.touch |
Keyboard, mouse and touch |
| XR controller buttons, sticks, hand joints and gestures | Input. |
XR controllers and hands |
| Tilt, device orientation or GPS position | Input., Input., Input. |
Motion sensors and location |
| Voice level, recordings, a webcam picture or a shared screen | Input., 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 |
In scripts
Section titled “In scripts”| Global | What it is |
|---|---|
Input | Every input device: keyboard, mouse, touch, XR, accelerometer, gyroscope, microphone, device camera, location. |
Key | Defines the values for standard keys |
MouseButton | The MouseButton enum provides constants representing the different mouse buttons. |
XRButton | |
Device | Platform facts for scripts that adapt to the device they run on. |
Haptics | Vibration on whatever the player has: XR controllers in an XR session, a gamepad's rumble when one is connected, the phone otherwise. |
Torch | The phone's torch, by its back camera: await Torch.on(), off(), toggle(), isOn(). |
WebBrowser | Read the browser location and open new tabs. |
ScreenGamepadComponent | An 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. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| Screen Gamepad | comp-screen-gamepad | An on-screen gamepad with two sticks and four buttons, for touch screens. |
In this section
Section titled “In this section”| 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. |