
# Objective When using empty events, it can feel redundant to have to specify the type of the event when sending it. ## Solution Add a new `fire()` function that sends the default value of the event. This requires that the event derives Default. Co-authored-by: Carter Anderson <mcanders1@gmail.com>
21 lines
613 B
Rust
21 lines
613 B
Rust
use crate::{
|
|
keyboard::{KeyCode, KeyboardInput},
|
|
ElementState,
|
|
};
|
|
use bevy_app::AppExit;
|
|
use bevy_ecs::prelude::{EventReader, EventWriter};
|
|
|
|
/// Sends the `AppExit` event whenever the "esc" key is pressed.
|
|
pub fn exit_on_esc_system(
|
|
mut keyboard_input_events: EventReader<KeyboardInput>,
|
|
mut app_exit_events: EventWriter<AppExit>,
|
|
) {
|
|
for event in keyboard_input_events.iter() {
|
|
if let Some(key_code) = event.key_code {
|
|
if event.state == ElementState::Pressed && key_code == KeyCode::Escape {
|
|
app_exit_events.send_default();
|
|
}
|
|
}
|
|
}
|
|
}
|