Skip to content

Commit

Permalink
Ignore synthetic key presses (emilk#4514)
Browse files Browse the repository at this point in the history
This PR discards "synthetic" winit keypresses, as discussed in the issue
emilk#4513.

* Closes emilk#4513
  • Loading branch information
hut authored and hacknus committed Oct 30, 2024
1 parent bad8543 commit e66da11
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub(crate) use profiling_scopes::*;

use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::ElementState,
event_loop::EventLoopWindowTarget,
window::{CursorGrabMode, Window, WindowButtons, WindowLevel},
};
Expand Down Expand Up @@ -368,16 +369,31 @@ 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) => {
Expand Down

0 comments on commit e66da11

Please sign in to comment.