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

Virtual Keycode is incorrect on macOS non-qwerty layouts #752

Closed
Toqozz opened this issue Jan 4, 2019 · 4 comments
Closed

Virtual Keycode is incorrect on macOS non-qwerty layouts #752

Toqozz opened this issue Jan 4, 2019 · 4 comments
Labels
C - in progress Implementation is proceeding smoothly DS - macos S - platform parity Unintended platform differences

Comments

@Toqozz
Copy link
Contributor

Toqozz commented Jan 4, 2019

The virtual_keycode field seems to be incorrect when using a non-qwerty layout. It seems to always return values corresponding to the qwerty (or perhaps physical?) layout.

I'm running the following program:

extern crate winit;

fn main() {
    let mut events_loop = winit::EventsLoop::new();
    let window = winit::Window::new(&events_loop).unwrap();

    events_loop.run_forever(|event| {
        println!("{:?}", event);
        match event {
            winit::Event::WindowEvent {
                event: winit::WindowEvent::CloseRequested, ..
            } => winit::ControlFlow::Break,
            _ => winit::ControlFlow::Continue,
        }
    });
}

Pressing a dvorak P here (qwerty 'R') results in the following lines:

WindowEvent { window_id: WindowId(Id(140217433672064)), event: KeyboardInput { device_id: DeviceId(DeviceId), input: KeyboardInput { scancode: 15, state: Pressed, virtual_keycode: Some(R), modifiers: ModifiersState { shift: false, ctrl: false, alt: false, logo: false } } } }

WindowEvent { window_id: WindowId(Id(140217433672064)), event: ReceivedCharacter('p') }

As you can see, we receive virtual_keycode: Some(R). Also note that right after the window receives the P character. (Shouldn't these always be the same?)

The issue doesn't seem to occur on Linux/X11.

winit version: 0.18
MacBook Pro (2017)
macOS version: 10.14 (Mojave)
@francesca64 francesca64 added H - help wanted Someone please save us DS - macos S - platform parity Unintended platform differences labels Jan 5, 2019
@francesca64
Copy link
Member

francesca64 commented Jan 5, 2019

Thanks for reporting this!

We're currently using NSEvent keyCode to generate VirtualKeyCode: https://github.com/tomaka/winit/blob/139686ddce870614f12eb3a0ed33a9adfee1e47a/src/platform/macos/view.rs#L415

This is wrong, since that's really just a scancode: https://stackoverflow.com/a/18413469/5435443

We likely inherited this oversight from GLFW, since it's done the same way there (and in SDL). It doesn't look like AppKit has a concept of layout-aware virtual key codes. WebKit has a good explanation and solution here: windowsKeyCodeForKeyEvent, windowsKeyCodeForCharCode

If you're interested in contributing, this would likely be an easy PR. Otherwise, it would be a week or so before I'd get a chance to implement this myself.

@Toqozz
Copy link
Contributor Author

Toqozz commented Jan 5, 2019

I will definitely have a look, thank you!

@francesca64
Copy link
Member

@chrisduerr are you aware of any Alacritty bugs that look like they're caused by this? (or any people who might want to test #755?)

@francesca64 francesca64 added C - in progress Implementation is proceeding smoothly and removed H - help wanted Someone please save us labels Jan 15, 2019
@chrisduerr
Copy link
Contributor

The issue that comes to mind is alacritty/alacritty#458. I've linked the PR there for anyone interested in testing.

francesca64 pushed a commit that referenced this issue Feb 23, 2019
* Fix incorrect keycodes when using a non-US keyboard layout.

This commit fixes the issue described in #752, and uses the advised
method to fix it.

* Style fixes

Co-Authored-By: Toqozz <[email protected]>

* Refactoring of macOS `virtualkeycode` fix (#752)

* Applies requested changes as per pull request discussion (#755).
elinorbgr pushed a commit to elinorbgr/winit that referenced this issue Apr 7, 2019
…dowing#755)

* Fix incorrect keycodes when using a non-US keyboard layout.

This commit fixes the issue described in rust-windowing#752, and uses the advised
method to fix it.

* Style fixes

Co-Authored-By: Toqozz <[email protected]>

* Refactoring of macOS `virtualkeycode` fix (rust-windowing#752)

* Applies requested changes as per pull request discussion (rust-windowing#755).
elinorbgr pushed a commit to elinorbgr/winit that referenced this issue Apr 7, 2019
…dowing#755)

* Fix incorrect keycodes when using a non-US keyboard layout.

This commit fixes the issue described in rust-windowing#752, and uses the advised
method to fix it.

* Style fixes

Co-Authored-By: Toqozz <[email protected]>

* Refactoring of macOS `virtualkeycode` fix (rust-windowing#752)

* Applies requested changes as per pull request discussion (rust-windowing#755).
@Osspial Osspial closed this as completed Apr 25, 2019
felixrabe pushed a commit to felixrabe/winit that referenced this issue Jun 30, 2019
…dowing#755)

* Fix incorrect keycodes when using a non-US keyboard layout.

This commit fixes the issue described in rust-windowing#752, and uses the advised
method to fix it.

* Style fixes

Co-Authored-By: Toqozz <[email protected]>

* Refactoring of macOS `virtualkeycode` fix (rust-windowing#752)

* Applies requested changes as per pull request discussion (rust-windowing#755).
kosyak pushed a commit to kosyak/winit that referenced this issue Jul 10, 2019
…dowing#755)

* Fix incorrect keycodes when using a non-US keyboard layout.

This commit fixes the issue described in rust-windowing#752, and uses the advised
method to fix it.

* Style fixes

Co-Authored-By: Toqozz <[email protected]>

* Refactoring of macOS `virtualkeycode` fix (rust-windowing#752)

* Applies requested changes as per pull request discussion (rust-windowing#755).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C - in progress Implementation is proceeding smoothly DS - macos S - platform parity Unintended platform differences
Development

No branches or pull requests

4 participants