Handle modifiers properly in winit-support

Previously only the "LeftCtrl" was set, not ModCtrl (same for shift etc). Although winit also act weirdly, and once the window regains focus it starts starts triggering ModifierChanged which also sets ModCtrl, so this problem wasn't always noticeable
This commit is contained in:
dbr 2023-02-01 13:23:46 +10:30
parent 842cd0700b
commit 0863512c43

View File

@ -291,6 +291,18 @@ fn to_imgui_key(keycode: VirtualKeyCode) -> Option<Key> {
}
}
fn handle_key_modifier(io: &mut Io, key: VirtualKeyCode, down: bool) {
if key == VirtualKeyCode::LShift || key == VirtualKeyCode::RShift {
io.add_key_event(imgui::Key::ModShift, down);
} else if key == VirtualKeyCode::LControl || key == VirtualKeyCode::RControl {
io.add_key_event(imgui::Key::ModCtrl, down);
} else if key == VirtualKeyCode::LAlt || key == VirtualKeyCode::RAlt {
io.add_key_event(imgui::Key::ModAlt, down);
} else if key == VirtualKeyCode::LWin || key == VirtualKeyCode::RWin {
io.add_key_event(imgui::Key::ModSuper, down);
}
}
impl WinitPlatform {
/// Initializes a winit platform instance and configures imgui.
///
@ -461,8 +473,18 @@ impl WinitPlatform {
},
..
} => {
let pressed = state == ElementState::Pressed;
// We map both left and right ctrl to `ModCtrl`, etc.
// imgui is told both "left control is pressed" and
// "consider the control key is pressed". Allows
// applications to use either general "ctrl" or a
// specific key. Same applies to other modifiers.
// https://github.com/ocornut/imgui/issues/5047
handle_key_modifier(io, key, pressed);
// Add main key event
if let Some(key) = to_imgui_key(key) {
let pressed = state == ElementState::Pressed;
io.add_key_event(key, pressed);
}
}