How to Handle Input in Bevy

Handle input in Bevy by querying the ButtonInput resource for key or mouse events in your update systems.

Bevy handles input by querying the ButtonInput resource for keyboard or mouse events within your game loop. Add the InputPlugin to your app and query the ButtonInput resource in a system to check for key presses.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, keyboard_input)
        .run();
}

fn keyboard_input(keyboard: Res<ButtonInput<KeyCode>>) {
    if keyboard.just_pressed(KeyCode::Space) {
        println!("Space pressed!");
    }
}