-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Currently, when running egui in X11 on Linux with the winit backend, phantom key presses are registered when the window gains focus. Examples:
- If you press Alt+TAB to select the window, a key press of "TAB" is erroneously registered.
- My window manager has the key binding Alt+w to change workspaces. Assuming the egui window has a text box selected, if I change the workspace to the one with the egui window with the key binding Alt+w, it will erroneously type a "w" into the text box.
In either example, I would expect no key to be registered. After all, no other (non-broken) application will register/type keys simply by changing focus into the application window.
This error happens because winit sends "fake" KeyboardInput events with a field "is_synthetic" when the window focus is gained. Key presses with "is_synthetic == true" should not be interpreted as real key presses and should be filtered out, according to the maintainers of winit.
egui currently does not do special handling of KeyboardInput events with "is_synthetic == true", see
egui/crates/egui-winit/src/lib.rs
Line 371 in 8321f64
| WindowEvent::KeyboardInput { event, .. } => { |
The proper handling would be to discard the event if "keyboardinput.event.state == ElementState::Pressed && keyboardinput.is_synthetic" is true.
See also:
- Relevant winit bug report: Keys leak through on window focus change (alt+tab) rust-windowing/winit#3691
- Relevant winit discussion: Remove synthetic key events? rust-windowing/winit#3543
- An analogous fix in another UI framework: fix: ignore synthetic keyboard input lapce/floem#387
- They discard all events with "
is_synthetic == true", which is another way to handle this.
- They discard all events with "
Thanks for looking into this. It probably causes a lot of confusion/frustration for X11 users that heavily customize their window manager key bindings.