observable_keyboard
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
- EventObservable - Observe events sent to an entity.
- MessageObservable -
Observe messages written via
MessageWriter. - ProxyObservable - Subscribe to another observable entity.
- ResourceObservable - Observe derived values of a resource on change.
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