Ignore synthetic key presses (#4514)

This PR discards "synthetic" winit keypresses, as discussed in the issue
#4513.

* Closes https://github.com/emilk/egui/issues/4513
This commit is contained in:
hut 2024-05-21 16:29:45 +00:00 committed by GitHub
parent 8321f64f6e
commit 262a8bcf98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 9 deletions

View File

@ -31,6 +31,7 @@ pub(crate) use profiling_scopes::*;
use winit::{ use winit::{
dpi::{PhysicalPosition, PhysicalSize}, dpi::{PhysicalPosition, PhysicalSize},
event::ElementState,
event_loop::EventLoopWindowTarget, event_loop::EventLoopWindowTarget,
window::{CursorGrabMode, Window, WindowButtons, WindowLevel}, window::{CursorGrabMode, Window, WindowButtons, WindowLevel},
}; };
@ -368,16 +369,31 @@ impl State {
consumed: self.egui_ctx.wants_keyboard_input(), consumed: self.egui_ctx.wants_keyboard_input(),
} }
} }
WindowEvent::KeyboardInput { event, .. } => { WindowEvent::KeyboardInput {
self.on_keyboard_input(event); 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. // When pressing the Tab key, egui focuses the first focusable element, hence Tab always consumes.
let consumed = self.egui_ctx.wants_keyboard_input() let consumed = self.egui_ctx.wants_keyboard_input()
|| event.logical_key || event.logical_key
== winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab); == winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab);
EventResponse { EventResponse {
repaint: true, repaint: true,
consumed, consumed,
}
} }
} }
WindowEvent::Focused(focused) => { WindowEvent::Focused(focused) => {