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::{
dpi::{PhysicalPosition, PhysicalSize},
event::ElementState,
event_loop::EventLoopWindowTarget,
window::{CursorGrabMode, Window, WindowButtons, WindowLevel},
};
@ -368,7 +369,21 @@ impl State {
consumed: self.egui_ctx.wants_keyboard_input(),
}
}
WindowEvent::KeyboardInput { event, .. } => {
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.
@ -380,6 +395,7 @@ impl State {
consumed,
}
}
}
WindowEvent::Focused(focused) => {
self.egui_input.focused = *focused;
self.egui_input