Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phantom key presses when focusing an egui/winit window #4513

Closed
hut opened this issue May 20, 2024 · 3 comments · Fixed by #4514
Closed

Phantom key presses when focusing an egui/winit window #4513

hut opened this issue May 20, 2024 · 3 comments · Fixed by #4514
Labels
bug Something is broken egui-winit porblems related to winit

Comments

@hut
Copy link
Contributor

hut commented May 20, 2024

Currently, when running egui in X11 on Linux with the winit backend, phantom key presses are registered when the window gains focus. Examples:

  1. If you press Alt+TAB to select the window, a key press of "TAB" is erroneously registered.
  2. 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

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:

Thanks for looking into this. It probably causes a lot of confusion/frustration for X11 users that heavily customize their window manager key bindings.

@hut hut added the bug Something is broken label May 20, 2024
@emilk
Copy link
Owner

emilk commented May 20, 2024

Sounds like an easy fix then! Wanna make a PR?

@emilk emilk added the egui-winit porblems related to winit label May 20, 2024
@hut
Copy link
Contributor Author

hut commented May 20, 2024

Sure, will do.

hut added a commit to hut/egui that referenced this issue May 20, 2024
emilk pushed a commit that referenced this issue May 21, 2024
This PR discards "synthetic" winit keypresses, as discussed in the issue
#4513.

* Closes #4513
@hut
Copy link
Contributor Author

hut commented May 21, 2024

Thanks so much for resolving this.

It's such a subtle bug, but so annoying. I'm using a tiling window manager with lots of alt+letter keys for focus switching, and all those unintended key presses are driving me crazy. Very relieved, thanks. <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken egui-winit porblems related to winit
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants