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

Opening a Window on keyboard_input.just_pressed makes it trigger again #12273

Open
DasLixou opened this issue Mar 3, 2024 · 1 comment · May be fixed by #12372
Open

Opening a Window on keyboard_input.just_pressed makes it trigger again #12273

DasLixou opened this issue Mar 3, 2024 · 1 comment · May be fixed by #12372
Labels
A-Input Player input via keyboard, mouse, gamepad, and more A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@DasLixou
Copy link
Contributor

DasLixou commented Mar 3, 2024

Bevy version

0.13

[Optional] Relevant system information

Windows 11

What you did

I made a system similar to the one in the keyboard_input example. But on the check whether the key A was just pressed, I then spawn a window.

fn spawn_debug_window(input: Res<ButtonInput<KeyCode>>, mut commands: Commands) {
    if input.just_pressed(KeyCode::F1) {
        info!("just pressed");
        commands.spawn(Window {
            title: "Debug".to_owned(),
            ..Default::default()
        });
    }
}

What went wrong

It spams opening new windows, the just_pressed is held for multiple frames.
I think this happens because it opens a new window, thus the focus is on the new window and the press counts as a new press?

2024-03-03T11:54:46.916247Z  INFO my_project::debug: just pressed
2024-03-03T11:54:46.930608Z  INFO bevy_winit::system: Creating new window "Debug" (25v4)
2024-03-03T11:54:46.967434Z  INFO my_project::debug: just pressed
2024-03-03T11:54:46.974474Z  INFO bevy_winit::system: Creating new window "Debug" (27v1)
2024-03-03T11:54:47.014095Z  INFO my_project::debug: just pressed
2024-03-03T11:54:47.036982Z  INFO bevy_winit::system: Creating new window "Debug" (29v1)
2024-03-03T11:54:47.078519Z  INFO my_project::debug: just pressed
2024-03-03T11:54:47.119463Z  INFO bevy_winit::system: Creating new window "Debug" (31v1)
@DasLixou DasLixou added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Mar 3, 2024
@TrialDragon TrialDragon added A-Input Player input via keyboard, mouse, gamepad, and more A-Windowing Platform-agnostic interface layer to run your app in S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Mar 3, 2024
@SpecificProtagonist
Copy link
Contributor

SpecificProtagonist commented Mar 6, 2024

When a new window is spawned, winit sends a key released event for to the old window and a key pressed event to the new one (tested without Bevy too).

I'm not sure if that's intentional. If not, we need to crate an issue in the winit repo; if it is, we need to work around this by ignoring key release events if in the same frame a window has been created and a key press event for the same button gets received.

Edit: These event happen when a window loses/gains focus. They have the is_synthetic flag set in that case. There's currently a discussion on whether winit should keep synthetic events: rust-windowing/winit#3543

So just_pressed and just_released are bugged. They trigger not just when the key was just pressed/released but also when focus was gained/lost with the key pressed.

We could just ignore synthetic events ourselves. The problem is that the button state can change while no Bevy window is focused. I think it might be best to wait until Bevy gains focus again to check whether to emit those events, and maybe keep the current behavior as a separate function.

github-merge-queue bot pushed a commit that referenced this issue Mar 8, 2024
# Objective

`ButtonInput<KeyCode>` documentation is currently incorrect/incomplete,
see #12273.

## Solution

Fix the documentation.

I think in the future we should also stop triggering
`just_pressed`/`just_released` when focus switches between two Bevy
windows, as those functions are independent of the window. It could also
make sense to add individual `ButtonInput<KeyCode>`s per window.

---------

Co-authored-by: Mateusz Wachowiak <mateusz_wachowiak@outlook.com>
spectria-limina pushed a commit to spectria-limina/bevy that referenced this issue Mar 9, 2024
# Objective

`ButtonInput<KeyCode>` documentation is currently incorrect/incomplete,
see bevyengine#12273.

## Solution

Fix the documentation.

I think in the future we should also stop triggering
`just_pressed`/`just_released` when focus switches between two Bevy
windows, as those functions are independent of the window. It could also
make sense to add individual `ButtonInput<KeyCode>`s per window.

---------

Co-authored-by: Mateusz Wachowiak <mateusz_wachowiak@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
3 participants