Skip to content

Commit

Permalink
egui-winit: Ignore synthetic key presses (emilk#4513)
Browse files Browse the repository at this point in the history
  • Loading branch information
hut committed May 20, 2024
1 parent 8321f64 commit 841978a
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 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,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) => {
Expand Down

0 comments on commit 841978a

Please sign in to comment.