From 0863512c43d552d6da40dbdb693f2aaa83ee59a1 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 1 Feb 2023 13:23:46 +1030 Subject: [PATCH] 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 --- imgui-winit-support/src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index caba629..47806cc 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -291,6 +291,18 @@ fn to_imgui_key(keycode: VirtualKeyCode) -> Option { } } +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); } }