mirror of
https://github.com/bevyengine/bevy
synced 2025-01-02 00:08:53 +00:00
27 lines
813 B
Rust
27 lines
813 B
Rust
use crate::keyboard::{ElementState, KeyCode, KeyboardInput};
|
|
use bevy_app::{
|
|
prelude::{EventReader, Events},
|
|
AppExit,
|
|
};
|
|
use bevy_ecs::{Local, Res, ResMut};
|
|
|
|
/// Local "exit on escape" system state
|
|
#[derive(Default)]
|
|
pub struct ExitOnEscapeState {
|
|
reader: EventReader<KeyboardInput>,
|
|
}
|
|
|
|
/// Sends the AppExit event whenever the "esc" key is pressed.
|
|
pub fn exit_on_esc_system(
|
|
mut state: Local<ExitOnEscapeState>,
|
|
keyboard_input_events: Res<Events<KeyboardInput>>,
|
|
mut app_exit_events: ResMut<Events<AppExit>>,
|
|
) {
|
|
for event in state.reader.iter(&keyboard_input_events) {
|
|
if let Some(key_code) = event.key_code {
|
|
if event.state == ElementState::Pressed && key_code == KeyCode::Escape {
|
|
app_exit_events.send(AppExit);
|
|
}
|
|
}
|
|
}
|
|
}
|