Hazel

Input

Your game reads the keyboard, the mouse and gamepads through input actions: named controls such as move, jump or interact. Scripts ask "is jump pressed?" instead of "is the space bar pressed?", so you can change which key does what, or add a gamepad button, without touching a script.

The actions every project starts with

A new project comes with these actions already set up:

ActionKindKeyboard and mouseGamepad
moveAxis2DWASD, and the arrow keysLeft stick
lookAxis2D(none)Right stick
jumpButtonSpaceSouth (A / Cross)
sprintButtonLeft ShiftWest (X / Square)
interactButtonEEast (B / Circle)
primaryButtonLeft mouse buttonRight trigger
secondaryButtonRight mouse buttonLeft trigger
accelerateButton(none)Right trigger
brakeButton(none)Left trigger

Any connected gamepad drives the actions, so a player can pick up whichever controller is nearest.

Kinds of action

  • Button: on or off, like jump or fire. A trigger bound to a button also reports how far it is squeezed, from 0 to 1.
  • Axis1D: one value from -1 to 1, like a throttle or a steering wheel.
  • Axis2D: a direction, like walking or aiming. Four keys (WASD) or a stick feed it, and it never goes past length 1, so diagonals are not faster.

Edit actions in Settings

Open Settings and pick Input under Controls.

To add an action, type a name under Add Action and click Button or Axis2D.

To bind a key or mouse button, expand the action in Input Actions, click + Add Binding, then press the key or click the mouse button you want. The prompt reads "Press any key or mouse button...". Press Escape or click Cancel to give up.

For a movement action (Axis2D), the WASD and Arrows buttons add the four keys in one click.

To remove a binding, click the bin icon beside it. Each action also has a Dead Zone: how far a stick must move before it counts, which stops a worn stick from drifting.

Every change is saved straight away to input_map.ron in your project folder, and the game loads that file when it starts.

Gamepad and modifier bindings

The listen prompt picks up ordinary keys and the left, right and middle mouse buttons. It ignores Shift, Ctrl and Alt, and it does not listen to gamepads. To bind one of those, open input_map.ron in the code editor and add it to the action's list of bindings:

(
    name: "dash",
    kind: Button,
    bindings: [
        Key("KeyQ"),
        GamepadButton("RightTrigger"),
    ],
    dead_zone: 0.15,
),
BindingExample names
Key("...")KeyW, Space, ShiftLeft, ArrowUp, Digit1, Escape
MouseButton("...")Left, Right, Middle
GamepadButton("...")South, East, West, North, LeftTrigger, RightTrigger, LeftTrigger2, RightTrigger2, Select, Start, LeftThumb, RightThumb, DPadUp, DPadDown, DPadLeft, DPadRight
GamepadAxis("...")LeftStickX, LeftStickY, RightStickX, RightStickY, LeftZ, RightZ
Composite2D(up: "...", down: "...", left: "...", right: "...")Four keys making one direction

LeftTrigger and RightTrigger are the bumpers; LeftTrigger2 and RightTrigger2 are the triggers. For an Axis2D action, binding either axis of a stick (say LeftStickX) uses the whole stick.

Read actions from a script

The current state of every action is in the ActionState resource. Ask it by name:

use bevy::prelude::*;
use renzora::{ActionState, ScriptCtx};

fn update(ctx: &mut ScriptCtx) {
    let dt = ctx.delta();
    let Some(input) = ctx.get_resource::<ActionState>() else { return };

    let walk = input.axis_2d("move");        // x: left/right, y: forward/back
    let running = input.pressed("sprint");    // held down
    let jumped = input.just_pressed("jump");  // only on the frame it went down

    let speed = if running { 8.0 } else { 4.0 };
    if let Some(mut transform) = ctx.get_mut::<Transform>() {
        transform.translation += Vec3::new(walk.x, 0.0, -walk.y) * speed * dt;
    }
    if jumped {
        info!("jump");
    }
}

renzora::script!(update);
MethodReturns
pressed("name")true while the action is held
just_pressed("name")true only on the frame it was pressed
just_released("name")true only on the frame it was let go
axis_1d("name")A number from -1 to 1 (for a Button, how far it is pressed, 0 to 1)
axis_2d("name")A direction (Vec2) of length 0 to 1

An action name that does not exist simply reads as not pressed.

New to scripts? Start with Scripting with Rust.

Let players rebind controls

A controls menu in your game can change bindings while the game runs. The map is the InputMap resource, and the game uses the new binding from the next frame:

use bevy::prelude::*;
use renzora::{InputBinding, InputMap, ScriptCtx};

fn update(ctx: &mut ScriptCtx) {
    let wants_f = ctx
        .get_resource::<ButtonInput<KeyCode>>()
        .is_some_and(|keys| keys.just_pressed(KeyCode::F1));
    if !wants_f {
        return;
    }
    if let Some(mut map) = ctx.get_resource_mut::<InputMap>() {
        // Slot 0 is the action's first binding. One past the last slot adds a new one.
        match map.rebind("jump", 0, Some(InputBinding::key(KeyCode::KeyF))) {
            Ok(()) => info!("Jump is now F"),
            Err(e) => warn!("{e}"),
        }
    }
}

renzora::script!(update);

Pass None instead of a binding to clear that slot. A rebind made in the game is not written to input_map.ron; save the player's choice yourself if it should survive a restart (see Saving).

Reading raw input

For something the action map does not cover, read Bevy's own input directly.

Keys and mouse buttons:

let Some(keys) = ctx.get_resource::<ButtonInput<KeyCode>>() else { return };
if keys.just_pressed(KeyCode::Tab) {
    info!("open the map");
}

let clicked = ctx
    .get_resource::<ButtonInput<MouseButton>>()
    .is_some_and(|mouse| mouse.just_pressed(MouseButton::Left));

Each gamepad separately, for local multiplayer: every connected controller is an entity with a Gamepad component.

use bevy::prelude::*;
use renzora::ScriptCtx;

fn update(ctx: &mut ScriptCtx) {
    let world = ctx.world();
    let mut pads = world.query::<&Gamepad>();
    for (index, pad) in pads.iter(world).enumerate() {
        let stick = pad.left_stick();
        if pad.just_pressed(GamepadButton::South) {
            info!("pad {index} pressed South, stick at {stick}");
        }
    }
}

renzora::script!(update);

Menus and gamepads

Buttons in your game's UI can be reached with a gamepad without any setup: the d-pad or left stick moves between them and South presses the one in focus. See Game UI.