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

Issue with number key mapping #3415

Closed
mkrueger opened this issue Sep 29, 2023 · 4 comments
Closed

Issue with number key mapping #3415

mkrueger opened this issue Sep 29, 2023 · 4 comments
Labels
bug Something is broken

Comments

@mkrueger
Copy link
Contributor

Seems I don't get egui::Event::Key events for the number keys when shift is pressed - works with any other modifier.
And shift seems to work with other keys as well, but the numbers:

        if ui.input(|i| i.key_pressed(egui::Key::Num1)) {
            print!("1 pressed!");
        }
@YgorSouza
Copy link
Contributor

The number keys stop being number keys when you hold shift. This is the code that converts the key variants from winit to egui:

Some(match key {
VirtualKeyCode::Down => Key::ArrowDown,
VirtualKeyCode::Left => Key::ArrowLeft,
VirtualKeyCode::Right => Key::ArrowRight,
VirtualKeyCode::Up => Key::ArrowUp,
VirtualKeyCode::Escape => Key::Escape,
VirtualKeyCode::Tab => Key::Tab,
VirtualKeyCode::Back => Key::Backspace,
VirtualKeyCode::Return | VirtualKeyCode::NumpadEnter => Key::Enter,
VirtualKeyCode::Space => Key::Space,
VirtualKeyCode::Insert => Key::Insert,
VirtualKeyCode::Delete => Key::Delete,
VirtualKeyCode::Home => Key::Home,
VirtualKeyCode::End => Key::End,
VirtualKeyCode::PageUp => Key::PageUp,
VirtualKeyCode::PageDown => Key::PageDown,
VirtualKeyCode::Minus | VirtualKeyCode::NumpadSubtract => Key::Minus,
// Using Mac the key with the Plus sign on it is reported as the Equals key
// (with both English and Swedish keyboard).
VirtualKeyCode::Equals | VirtualKeyCode::Plus | VirtualKeyCode::NumpadAdd => {
Key::PlusEquals
}
VirtualKeyCode::Key0 | VirtualKeyCode::Numpad0 => Key::Num0,
VirtualKeyCode::Key1 | VirtualKeyCode::Numpad1 => Key::Num1,
VirtualKeyCode::Key2 | VirtualKeyCode::Numpad2 => Key::Num2,
VirtualKeyCode::Key3 | VirtualKeyCode::Numpad3 => Key::Num3,
VirtualKeyCode::Key4 | VirtualKeyCode::Numpad4 => Key::Num4,
VirtualKeyCode::Key5 | VirtualKeyCode::Numpad5 => Key::Num5,
VirtualKeyCode::Key6 | VirtualKeyCode::Numpad6 => Key::Num6,
VirtualKeyCode::Key7 | VirtualKeyCode::Numpad7 => Key::Num7,
VirtualKeyCode::Key8 | VirtualKeyCode::Numpad8 => Key::Num8,
VirtualKeyCode::Key9 | VirtualKeyCode::Numpad9 => Key::Num9,
VirtualKeyCode::A => Key::A,
VirtualKeyCode::B => Key::B,
VirtualKeyCode::C => Key::C,
VirtualKeyCode::D => Key::D,
VirtualKeyCode::E => Key::E,
VirtualKeyCode::F => Key::F,
VirtualKeyCode::G => Key::G,
VirtualKeyCode::H => Key::H,
VirtualKeyCode::I => Key::I,
VirtualKeyCode::J => Key::J,
VirtualKeyCode::K => Key::K,
VirtualKeyCode::L => Key::L,
VirtualKeyCode::M => Key::M,
VirtualKeyCode::N => Key::N,
VirtualKeyCode::O => Key::O,
VirtualKeyCode::P => Key::P,
VirtualKeyCode::Q => Key::Q,
VirtualKeyCode::R => Key::R,
VirtualKeyCode::S => Key::S,
VirtualKeyCode::T => Key::T,
VirtualKeyCode::U => Key::U,
VirtualKeyCode::V => Key::V,
VirtualKeyCode::W => Key::W,
VirtualKeyCode::X => Key::X,
VirtualKeyCode::Y => Key::Y,
VirtualKeyCode::Z => Key::Z,
VirtualKeyCode::F1 => Key::F1,
VirtualKeyCode::F2 => Key::F2,
VirtualKeyCode::F3 => Key::F3,
VirtualKeyCode::F4 => Key::F4,
VirtualKeyCode::F5 => Key::F5,
VirtualKeyCode::F6 => Key::F6,
VirtualKeyCode::F7 => Key::F7,
VirtualKeyCode::F8 => Key::F8,
VirtualKeyCode::F9 => Key::F9,
VirtualKeyCode::F10 => Key::F10,
VirtualKeyCode::F11 => Key::F11,
VirtualKeyCode::F12 => Key::F12,
VirtualKeyCode::F13 => Key::F13,
VirtualKeyCode::F14 => Key::F14,
VirtualKeyCode::F15 => Key::F15,
VirtualKeyCode::F16 => Key::F16,
VirtualKeyCode::F17 => Key::F17,
VirtualKeyCode::F18 => Key::F18,
VirtualKeyCode::F19 => Key::F19,
VirtualKeyCode::F20 => Key::F20,
_ => {
return None;
}
})
}

I put a dbg! on the key variable, and when holding shift, it sends At when pressing 2, Asterisk when pressing 8, and nothing for the other keys, because apparently winit doesn't have variants for them either.

@mkrueger
Copy link
Contributor Author

mkrueger commented Oct 5, 2023

y it's surely not easy. I've worked on editors for 20+ years and my solution was decomposing keys for that. Was always a bit of a mess. But on application level sometimes it's required unfortunately.

There is a conflict between shortcut keys and text input that's not really modelled well on OS level.

The hacky solution we used for gtk doesn't help here, but shows the way:
https://github.com/mono/monodevelop/blob/main/main/src/core/MonoDevelop.Ide/MonoDevelop.Components/GtkWorkarounds.cs#L485

For solving that you need access to something like that:
https://docs.gtk.org/gdk3/class.Keymap.html

The raw keys get mapped to real keys let's say shift+1 maps to ! and on app level you get '!' - that needs to be taken back. It's tricky and unfortunately I don't know enough about the VirtualKeyCode thing there to be really helpful.

@mkrueger
Copy link
Contributor Author

0.25 improved key input but I stll don't get a key event for shift+number - only the text event.

@emilk
Copy link
Owner

emilk commented Jan 15, 2024

key_pressed checks for logical keys. Shift-1 is is the logical key ! on my keyboard (and we have no Key::ExclamationMark in egui).

You should get an Event::Key with physical_key: Key::Num1 when you press shift-1.

#3653 explains physical vs logical keys.

See https://docs.rs/egui/latest/egui/enum.Event.html#variant.Key.field.physical_key

The docs could be improved here, but as you see, physical keys are not yet implemented in eframe web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants