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); } }