Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

observable_keyboard

crates.io ci codecov license

The KeyboardObservable turns Bevy keyboard input events into signals. The events are sourced from the ButtonInput<KeyCode> resource.

Options

KeyCode signals can be observed in multiple modes:

  • KeyboardObservableEmit::JustPressed - emits once when the key is pressed down.
  • KeyboardObservableEmit::JustReleased - emits once when the key is released.
  • KeyboardObservableEmit::WhilePressed - emits continuously while the key is held down.

See Also

Example

cargo run -p rx_bevy --example observable_keyboard_example
fn main() -> AppExit {
    App::new()
        .add_plugins((
            DefaultPlugins,
            RxPlugin,
            RxSchedulerPlugin::<Update, Virtual>::default(),
        ))
        .add_systems(Startup, setup)
        .add_systems(
            Update,
            (
                send_message(AppExit::Success).run_if(input_just_pressed(KeyCode::Escape)),
                unsubscribe.run_if(input_just_pressed(KeyCode::Space)),
            ),
        )
        .run()
}

fn unsubscribe(mut example_entities: ResMut<MySubscriptions>) {
    example_entities.subscription.unsubscribe();
}

#[derive(Resource)]
struct MySubscriptions {
    subscription: SharedSubscription,
}

fn setup(mut commands: Commands, rx_schedule_update_virtual: RxSchedule<Update, Virtual>) {
    let subscription = KeyboardObservable::new(default(), rx_schedule_update_virtual.handle())
        .subscribe(PrintObserver::new("keyboard"));

    commands.insert_resource(MySubscriptions {
        subscription: SharedSubscription::new(subscription),
    });
}

Output when pressing WASD keys and Space:

keyboard - next: KeyW
keyboard - next: KeyA
keyboard - next: KeyS
keyboard - next: KeyD
keyboard - next: Space
keyboard - unsubscribed