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

web: Input is broken on international or non-qwerty keyboard layouts #1

Closed
rparrett opened this issue Jan 5, 2021 · 6 comments
Closed

Comments

@rparrett
Copy link
Owner

rparrett commented Jan 5, 2021

This is due to a workaround for an issue that was causing the backspace key to be super janky. That issue may be in bevy and/or winit.

taipo/src/typing.rs

Lines 400 to 413 in ebcffca

// We were previously using Res<Events<ReceivedCharacter>> to handle the ascii bits,
// and Res<Events<KeyboardInput>> to handle backspace/enter, but there was something
// wacky going on where backspace could end up coming in out of order.
//
// After testing using puppeteer to shove various keyboard inputs in, it seems like
// this solution, though ugly, results in a better typing experience.
//
// I had also attempted to get ReceivedCharacter to give me backspace/enter, but that
// was not working, despite winit docs seeming to suggest that it should. But I found
// that I received no ReceivedCharacter events at all when typing backspace/enter.
//
// I'm guessing that the ReceivedCharacter approach would be ideal though if this
// solution doesn't work for people with non-english keyboards or dvorak layouts or
// whatever.

@rparrett
Copy link
Owner Author

rparrett commented Jan 5, 2021

The answer is probably in here somewhere: rust-windowing/winit#1806

@rparrett
Copy link
Owner Author

rparrett commented Jan 11, 2021

enter/backspace seem to produce ReceivedCharacter events in non-web scenarios (running the bevy "char_input_events" example) but not in this game / in wasm builds.

typing_system should be as simple as this, but it just does not work.

#[derive(Default)]
pub struct ReceivedCharacterState {
    event_reader: EventReader<ReceivedCharacter>,
}
fn typing_system(
    mut typing_state: ResMut<TypingState>,
    mut state: Local<ReceivedCharacterState>,
    char_input_events: Res<Events<ReceivedCharacter>>,
) {
    for event in state.event_reader.iter(&char_input_events) {
        info!("{:?}", event.char);
        if event.char == '\u{7f}' {
            typing_state.buf.pop();
        } else if event.char == '\r' {
            let text = typing_state.buf.clone();

            typing_state.buf.clear();
            typing_submit_events.send(TypingSubmitEvent { text });
        } else {
            typing_state.buf.push(event.char);
        }
    }
}

As far as I can tell, bevy_winit just passes ReceivedCharacter events straight through from winit, so it seems like it must be a winit issue.

@rparrett
Copy link
Owner Author

rparrett commented Jan 11, 2021

It appears at a glance that winit just passes along keypress events.

https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event

And while enter works in that example, backspace does not. (Chrome)

So that angle seems pretty hopeless.

However, it looks like KeyBoardInputEvent has a scan_code property that we could be using instead of key_code. When cast as a char, this seems to produce the correct letters when typing qwertz with a qwertz layout.

I am not actually sure what this "scan code" is though, and the results when typing punctuation seem mysterious. (particularly - which we need.)

@rparrett
Copy link
Owner Author

rparrett commented Sep 2, 2022

Blocked on rust-windowing/winit#1888

@rparrett
Copy link
Owner Author

Just checked on this again, and winit (main branch) is still using keypress, and backspace is still not being passed along.

If we want to support non-qwerty layouts right now, it probably needs to be by allowing the user to choose the layout in a settings menu.

@rparrett rparrett changed the title Input is broken on international or non-qwerty keyboard layouts [web] Input is broken on international or non-qwerty keyboard layouts Dec 14, 2022
@rparrett
Copy link
Owner Author

rparrett commented Mar 19, 2024

Fixed! It's now possible to get character data form Bevy's EventReader<KeyboardInput>. We still can't use ReceivedCharacter due to inconsistencies with backspace between web/native builds.

@rparrett rparrett changed the title [web] Input is broken on international or non-qwerty keyboard layouts web: Input is broken on international or non-qwerty keyboard layouts Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant