Skip to content

Commit

Permalink
Clear modifier state when focus is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
SludgePhD committed Jul 21, 2024
1 parent 6705757 commit 6916245
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
system::{Local, Res, SystemParam},
},
input::{
keyboard::{Key, KeyCode, KeyboardInput},
keyboard::{Key, KeyCode, KeyboardFocusLost, KeyboardInput},
mouse::{MouseButton, MouseButtonInput, MouseScrollUnit, MouseWheel},
touch::TouchInput,
ButtonState,
Expand All @@ -29,16 +29,18 @@ pub struct InputEvents<'w, 's> {
pub ev_mouse_wheel: EventReader<'w, 's, MouseWheel>,
pub ev_keyboard_input: EventReader<'w, 's, KeyboardInput>,
pub ev_touch: EventReader<'w, 's, TouchInput>,
pub ev_focus: EventReader<'w, 's, KeyboardFocusLost>,
}

impl<'w, 's> InputEvents<'w, 's> {
/// Consumes all the events.
pub fn clear(&mut self) {
self.ev_cursor.read().last();
self.ev_mouse_button_input.read().last();
self.ev_mouse_wheel.read().last();
self.ev_keyboard_input.read().last();
self.ev_touch.read().last();
self.ev_cursor.clear();
self.ev_mouse_button_input.clear();
self.ev_mouse_wheel.clear();
self.ev_keyboard_input.clear();
self.ev_touch.clear();
self.ev_focus.clear();
}
}

Expand Down Expand Up @@ -144,6 +146,12 @@ pub fn process_input_system(
};
}

// If window focus is lost, clear all modifiers to avoid stuck keys.
if !input_events.ev_focus.is_empty() {
input_events.ev_focus.clear();
*input_resources.modifier_keys_state = Default::default();
}

let ModifierKeysState {
shift,
ctrl,
Expand Down

0 comments on commit 6916245

Please sign in to comment.