From b92fa3ca5ef2318cdbda2d165a56b44ea218696d Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Wed, 12 Apr 2017 15:46:28 +0200 Subject: [PATCH] Fix bug causing inconsistent imgui state when pressing the enter key --- modules/onscreengui/src/gui.cpp | 53 ++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/modules/onscreengui/src/gui.cpp b/modules/onscreengui/src/gui.cpp index 1b533dbf7a..933406f5b1 100644 --- a/modules/onscreengui/src/gui.cpp +++ b/modules/onscreengui/src/gui.cpp @@ -493,24 +493,43 @@ bool GUI::mouseWheelCallback(double position) { } bool GUI::keyCallback(Key key, KeyModifier modifier, KeyAction action) { - ImGuiIO& io = ImGui::GetIO(); - bool consumeEvent = io.WantCaptureKeyboard; - if (consumeEvent) { - int keyIndex = static_cast(key); - if (keyIndex < 0) { - LERROR("Pressed key of index '" << keyIndex << "' was negative"); - } - else { - if (action == KeyAction::Press) - io.KeysDown[keyIndex] = true; - if (action == KeyAction::Release) - io.KeysDown[keyIndex] = false; - } - - io.KeyShift = hasKeyModifier(modifier, KeyModifier::Shift); - io.KeyCtrl = hasKeyModifier(modifier, KeyModifier::Control); - io.KeyAlt = hasKeyModifier(modifier, KeyModifier::Alt); + const int keyIndex = static_cast(key); + if (keyIndex < 0) { + LERROR("Key of index '" << keyIndex << "' was negative"); + return false; } + + const bool hasShift = hasKeyModifier(modifier, KeyModifier::Shift); + const bool hasCtrl = hasKeyModifier(modifier, KeyModifier::Control); + const bool hasAlt = hasKeyModifier(modifier, KeyModifier::Alt); + + ImGuiIO& io = ImGui::GetIO(); + + const bool consumeEvent = io.WantCaptureKeyboard; + if (consumeEvent) { + if (action == KeyAction::Press) { + io.KeysDown[keyIndex] = true; + } + io.KeyShift = hasShift; + io.KeyCtrl = hasCtrl; + io.KeyAlt = hasAlt; + } + + // Even if the event is not consumed, + // set keys and modifiers to false when they are released. + if (action == KeyAction::Release) { + io.KeysDown[keyIndex] = false; + } + if (!hasShift) { + io.KeyShift = false; + } + if (!hasCtrl) { + io.KeyCtrl = false; + } + if (!hasAlt) { + io.KeyAlt = false; + } + return consumeEvent; }