diff --git a/src/app.rs b/src/app.rs index 0fa9c21..9796089 100644 --- a/src/app.rs +++ b/src/app.rs @@ -258,6 +258,10 @@ impl ApplicationHandler for App { self.state.window.is_focused = focused; if focused { self.state.window.request_redraw(); + } else { + // Clear inputs on lost focus to avoid "stuck" keys on Wayland + // systems. + self.state.input.clear_pressed(); } } WindowEvent::ScaleFactorChanged { scale_factor, .. } => { diff --git a/src/input.rs b/src/input.rs index ae34280..ee39023 100644 --- a/src/input.rs +++ b/src/input.rs @@ -77,6 +77,14 @@ impl InputState { pub fn set_mouse_pos(&mut self, pos: LogicalPoint) { self.cursor_pos = pos; } + + /// Clears all pressed keys, buttons, and modifier states. Used when the + /// application loses focus to avoid keys being "stuck" on/pressed. + pub fn clear_pressed(&mut self) { + self.keys_pressed.clear(); + self.mouse_pressed.clear(); + self.key_modifiers = ModifiersState::empty(); + } } /// Attempts to convert the provided string `s` to a [KeyCode].