Skip to content

Commit

Permalink
Don't clear modifier state on focus change (emilk#4157)
Browse files Browse the repository at this point in the history
I believe that the underlying issue that caused the stuck modifier keys
was resolved in the 0.29 winit keyboard refactor.

This probably needs to tested on other desktop platforms however since I
am only able to test this on windows 11.

* Closes <emilk#2332>
  • Loading branch information
ming08108 authored and hacknus committed Oct 30, 2024
1 parent 619d7dd commit 36b3493
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 18 deletions.
3 changes: 0 additions & 3 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,6 @@ impl State {
}
WindowEvent::Focused(focused) => {
self.egui_input.focused = *focused;
// We will not be given a KeyboardInput event when the modifiers are released while
// the window does not have focus. Unset all modifier state to be safe.
self.egui_input.modifiers = egui::Modifiers::default();
self.egui_input
.events
.push(egui::Event::WindowFocused(*focused));
Expand Down
16 changes: 1 addition & 15 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,6 @@ impl InputState {
}
}

let mut modifiers = new.modifiers;

let focused_changed = self.focused != new.focused
|| new
.events
.iter()
.any(|e| matches!(e, Event::WindowFocused(_)));
if focused_changed {
// It is very common for keys to become stuck when we alt-tab, or a save-dialog opens by Ctrl+S.
// Therefore we clear all the modifiers and down keys here to avoid that.
modifiers = Default::default();
keys_down = Default::default();
}

Self {
pointer,
touch_states: self.touch_states,
Expand All @@ -278,7 +264,7 @@ impl InputState {
predicted_dt: new.predicted_dt,
stable_dt,
focused: new.focused,
modifiers,
modifiers: new.modifiers,
keys_down,
events: new.events.clone(), // TODO(emilk): remove clone() and use raw.events
raw: new,
Expand Down

0 comments on commit 36b3493

Please sign in to comment.