Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear modifier state when focus is lost #298

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading