bevy
bevy copied to clipboard
Key press input repeats forever after losing window focus
Bevy version
0.5.0 commit b07db84
Operating system & version
Arch Linux, X11
What you did
I was making a movable sprite with the WASD and while moving it I clicked on the editor to return to the code and noticed the sprite kept moving in the last direction I was pressing before changing windows. The only way to stop it even after giving focus back to it was by re-pressing those last keys I was keeping pressed.
What you expected to happen
I expected for key presses to stop as soon as the game window lost focus.
What actually happened
The paddle continues forever in the direction pressed even when returning focus on the game. It looks like the game is stuck on sending key presses events (might be winit fault and not bevy?)
Additional information
It can be reproduced on the breakout example (just set a lower paddle speed, like 100, to see it better). Hold Left or Right for 1-2 seconds and click outside the window to give focus to something else.
This is probably a bug in winit.
Did you use EventReader<KeyboardInput>>? If so it kind of makes sense. When you switched focus, the key up event wouldn't have been sent to the game window as it is no longer focused. Instead it would have been sent to your editor.
I used Res<Input<KeyCode>> (I just copied from breakout game example), so code like this
if keyboard_input.pressed(KeyCode::A) {
dir.x += -1.0;
}
will always be true and ran when you press A and change window.
I guess Bevy should unset all keyboard inputs when focus is lost.
on macOS, I receive the released event even if the window is no longer in focus:
- if the window has the focus when the press event happen, it will receive the release event even if it lost focus
- if the window doesn't have the focus when the press event happen, it won't receive the release event even if it gain focus
could someone check on Windows and Wayland how that works?
I experienced an issue like this on Windows, but only if you release the button while dragging the window. This is using if keyboard_input.pressed(KeyCode::...) {...}
Correct behavior with losing focus, also regaining focus:
- press a key -> pressed is true
- click outside the window to unfocus the window -> pressed is false
- click back inside the window to focus the window -> pressed is true
- release the key -> pressed is false
Correct behavior with dragging the window:
- press a key -> pressed is true
- begin dragging the window with the mouse -> pressed is true
- release the mouse -> pressed is true
- release the key -> pressed is false
Incorrect behavior with dragging the window:
- press a key -> pressed is true
- begin dragging the window with the mouse -> pressed is true
- release the key -> pressed is still true (BUG)
- release the mouse -> pressed is still true (BUG)
- click outside the window to unfocus the window -> pressed is still true (BUG)
- click the window to refocus it -> pressed is still true (BUG)
- press and release a different key -> original key pressed is still true (BUG)
- press and release the key again -> pressed is false
Looks like the only way to un-press the key is to press it again so that Bevy receives a key-release event.
I've got the same issue with a game running in the browser via wasm export.
Same issue (in wasm also). Fun thing is that some other lib code actually respects focus, notably my collision detection, which allows users to bypass collisions by doing alt+tab!
This still seems to be an issue in 0.12 - Chrome + MacOS.
Could this be fixed with the new winit change? + @Vrixyz