From 841978ad51084a23db7e612f1b06caef111c2b00 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 20 May 2024 21:17:02 +0200 Subject: [PATCH 1/2] egui-winit: Ignore synthetic key presses (#4513) --- crates/egui-winit/src/lib.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 058a27f1661..193d8030fae 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -31,6 +31,7 @@ pub(crate) use profiling_scopes::*; use winit::{ dpi::{PhysicalPosition, PhysicalSize}, + event::ElementState, event_loop::EventLoopWindowTarget, window::{CursorGrabMode, Window, WindowButtons, WindowLevel}, }; @@ -368,16 +369,27 @@ impl State { consumed: self.egui_ctx.wants_keyboard_input(), } } - WindowEvent::KeyboardInput { event, .. } => { - self.on_keyboard_input(event); - - // When pressing the Tab key, egui focuses the first focusable element, hence Tab always consumes. - let consumed = self.egui_ctx.wants_keyboard_input() - || event.logical_key - == winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab); - EventResponse { - repaint: true, - consumed, + WindowEvent::KeyboardInput { event, is_synthetic, .. } => { + // Winit generates fake "synthetic" KeyboardInput events when the focus + // is changed to the window, or away from it. Synthetic key presses + // represent no real key presses and should be ignored. + // See https://github.com/rust-windowing/winit/issues/3543 + if *is_synthetic && event.state == ElementState::Pressed { + EventResponse { + repaint: true, + consumed: false, + } + } else { + self.on_keyboard_input(event); + + // When pressing the Tab key, egui focuses the first focusable element, hence Tab always consumes. + let consumed = self.egui_ctx.wants_keyboard_input() + || event.logical_key + == winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab); + EventResponse { + repaint: true, + consumed, + } } } WindowEvent::Focused(focused) => { From 262e6b8d98cabe9ad9344903436fa6180ec5aa8a Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 21 May 2024 16:34:05 +0200 Subject: [PATCH 2/2] apply cargo fmt --- crates/egui-winit/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 193d8030fae..fec16c73ae8 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -369,7 +369,11 @@ impl State { consumed: self.egui_ctx.wants_keyboard_input(), } } - WindowEvent::KeyboardInput { event, is_synthetic, .. } => { + WindowEvent::KeyboardInput { + event, + is_synthetic, + .. + } => { // Winit generates fake "synthetic" KeyboardInput events when the focus // is changed to the window, or away from it. Synthetic key presses // represent no real key presses and should be ignored.