Input
How the NEO engine reads keyboards, mice, gamepads and touch screens, and how pointers pick entities on screen.
The engine turns what the operating system or the browser reports into resources, components and messages your systems read every frame. DefaultPlugins adds all of it: InputPlugin for the devices, the gamepad backend, and the picking plugins that tell you which entity a pointer is over.
Three ways to read input#
| Way | What you use | When to use it |
|---|---|---|
| State | Res<ButtonInput<KeyCode>>, Res<ButtonInput<MouseButton>>, the Gamepad component, Res<Touches> | Most gameplay: "is jump held?", "was fire just pressed this frame?" |
| Messages | MessageReader<KeyboardInput>, MessageReader<MouseMotion>, MessageReader<TouchInput> and others | Every change in order, for text entry, rebinding screens and recording input. |
| Run conditions | input_just_pressed(KeyCode::F3), input_pressed(..), input_toggle_active(..) | Running a whole system only when a key is down. |
State is updated in PreUpdate, in the InputSystems set, so systems in Update see this frame's input.
use neo::input::common_conditions::input_just_pressed;
use neo::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, (jump, toggle_menu.run_if(input_just_pressed(KeyCode::Escape))))
.run();
}
fn jump(keys: Res<ButtonInput<KeyCode>>) {
if keys.just_pressed(KeyCode::Space) {
info!("jump");
}
}
fn toggle_menu() {
info!("menu");
}Pages#
| Page | What it covers |
|---|---|
| Keyboard | Key codes and logical keys, modifiers, text and keyboard messages. |
| Mouse | Buttons, motion, the wheel, the cursor's position and grabbing the cursor. |
| Gamepad | Gamepads as entities, buttons and sticks, dead zones and rumble. |
| Touch | Touches on phones and tablets, and NEO's on-screen controls. |
| Picking | Pointer events on meshes, sprites and UI: hover, click and drag. |
Focus and keyboard navigation between UI elements are in Focus and navigation.
In NEO games#
NEO's games don't read devices directly for the character. The input kit, neo_input_kit, maps keyboard, mouse, gamepad and, on phones, touch onto one character input, with the Starter template's bindings, a dead zone and an InputGate that blocks it while a menu is open. Read Gameplay kit before writing your own mapping; your own systems can still read any device for keys the kit doesn't use.