Skip to content

Commit

Permalink
Improve code quality of exit_on_esc_system (#4260)
Browse files Browse the repository at this point in the history
# Objective

- exit_on_esc system uses a slow and non-standard way to check for inputs

## Solution

- Use Input resource, rather than iterating events

## Context

Alternative to #4259.
  • Loading branch information
alice-i-cecile committed May 2, 2022
1 parent 2c45c3c commit 3bb8bcd
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions crates/bevy_input/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::input::Input;
use crate::keyboard::KeyCode;
use crate::{ButtonState, KeyCode, KeyboardInput};
use bevy_app::AppExit;
use bevy_ecs::prelude::{EventReader, EventWriter};
use bevy_ecs::prelude::{EventWriter, Res};

/// Sends an [`AppExit`] event whenever the `ESC` key is pressed.
///
Expand All @@ -9,14 +11,10 @@ use bevy_ecs::prelude::{EventReader, EventWriter};
/// This system is not added as part of the `DefaultPlugins`. You can add the [`exit_on_esc_system`]
/// yourself if desired.
pub fn exit_on_esc_system(
mut keyboard_input_events: EventReader<KeyboardInput>,
keyboard_input: Res<Input<KeyCode>>,
mut app_exit_events: EventWriter<AppExit>,
) {
for event in keyboard_input_events.iter() {
if let Some(key_code) = event.key_code {
if event.state == ButtonState::Pressed && key_code == KeyCode::Escape {
app_exit_events.send_default();
}
}
}
if keyboard_input.just_pressed(KeyCode::Escape) {
app_exit_events.send_default();
}
}

0 comments on commit 3bb8bcd

Please sign in to comment.