bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.04k stars 3.44k forks source link

Key press input repeats forever after losing window focus #2068

Open giusdp opened 3 years ago

giusdp commented 3 years ago

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.

bjorn3 commented 3 years ago

This is probably a bug in winit.

bjorn3 commented 3 years ago

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.

giusdp commented 3 years ago

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.

bjorn3 commented 3 years ago

I guess Bevy should unset all keyboard inputs when focus is lost.

mockersf commented 3 years ago

on macOS, I receive the released event even if the window is no longer in focus:

could someone check on Windows and Wayland how that works?

pixelprizm commented 3 years ago

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:

Correct behavior with dragging the window:

Incorrect behavior with dragging the window:

Looks like the only way to un-press the key is to press it again so that Bevy receives a key-release event.

Th0rgal commented 1 year ago

I've got the same issue with a game running in the browser via wasm export.

dearlordylord commented 1 year ago

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!

simbleau commented 7 months ago

This still seems to be an issue in 0.12 - Chrome + MacOS.

Could this be fixed with the new winit change? + @Vrixyz