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 19, 2024
1 parent 6705757 commit b74683d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 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 Expand Up @@ -226,7 +234,7 @@ pub fn process_input_system(
});
}

for event in keyboard_input_events {
for event in input_events.ev_keyboard_input.read() {
let text_event_allowed = !command && !win || !*context_params.is_macos && ctrl && alt;
let Some(mut window_context) = context_params.window_context(event.window) else {
continue;
Expand Down

0 comments on commit b74683d

Please sign in to comment.