From 966363a8599c7b19383de939b5f4d0670a1dc862 Mon Sep 17 00:00:00 2001 From: Ac5000 <15003210+Ac5000@users.noreply.github.com> Date: Sat, 28 Feb 2026 00:08:35 -0500 Subject: [PATCH] Added method to InputState to clear pressed keys, mods, and mouse. Call method on window focus loss. --- src/app.rs | 4 ++++ src/input.rs | 8 ++++++++ 2 files changed, 12 insertions(+) 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].