bevy/crates/bevy_input/src/system.rs

24 lines
1,022 B
Rust
Raw Normal View History

2020-04-25 01:55:15 +00:00
use crate::keyboard::{ElementState, KeyboardInput, VirtualKeyCode};
use bevy_app::{AppExit, Events, GetEventReader};
2020-04-19 19:13:04 +00:00
use legion::prelude::*;
pub fn exit_on_esc_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
SystemBuilder::new("exit_on_esc")
.read_resource::<Events<KeyboardInput>>()
.write_resource::<Events<AppExit>>()
2020-04-25 01:55:15 +00:00
.build(
move |_, _, (ref keyboard_input_events, ref mut app_exit_events), _| {
for event in keyboard_input_event_reader.iter(keyboard_input_events) {
2020-04-25 01:55:15 +00:00
if let Some(virtual_key_code) = event.virtual_key_code {
if event.state == ElementState::Pressed
&& virtual_key_code == VirtualKeyCode::Escape
{
app_exit_events.send(AppExit);
}
2020-04-19 19:13:04 +00:00
}
}
2020-04-25 01:55:15 +00:00
},
)
}