From 4b7be31197ca3bad44ec9d9a12a94001ab097b74 Mon Sep 17 00:00:00 2001 From: Dale Kim Date: Fri, 3 Apr 2015 09:01:48 -0500 Subject: [PATCH 1/2] Fixed modifier key state setting in GLFW callbacks. --- .../opengl3_example/imgui_impl_glfw_gl3.cpp | 44 ++++++++++++++++--- examples/opengl_example/imgui_impl_glfw.cpp | 44 ++++++++++++++++--- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp index 1cd2faf0..fa756500 100644 --- a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp +++ b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp @@ -138,12 +138,46 @@ void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow*, int key, int, int action, int mo { ImGuiIO& io = ImGui::GetIO(); if (action == GLFW_PRESS) - io.KeysDown[key] = true; + { + switch (key) + { + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: + io.KeyCtrl = true; + break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + io.KeyShift = true; + break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: + io.KeyAlt = true; + break; + default: + io.KeysDown[key] = true; + } + } + if (action == GLFW_RELEASE) - io.KeysDown[key] = false; - io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; - io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; - io.KeyAlt = (mods & GLFW_MOD_ALT) != 0; + { + switch (key) + { + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: + io.KeyCtrl = false; + break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + io.KeyShift = false; + break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: + io.KeyAlt = false; + break; + default: + io.KeysDown[key] = false; + } + } } void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow*, unsigned int c) diff --git a/examples/opengl_example/imgui_impl_glfw.cpp b/examples/opengl_example/imgui_impl_glfw.cpp index 8c9cb216..3349a266 100644 --- a/examples/opengl_example/imgui_impl_glfw.cpp +++ b/examples/opengl_example/imgui_impl_glfw.cpp @@ -119,12 +119,46 @@ void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods) { ImGuiIO& io = ImGui::GetIO(); if (action == GLFW_PRESS) - io.KeysDown[key] = true; + { + switch (key) + { + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: + io.KeyCtrl = true; + break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + io.KeyShift = true; + break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: + io.KeyAlt = true; + break; + default: + io.KeysDown[key] = true; + } + } + if (action == GLFW_RELEASE) - io.KeysDown[key] = false; - io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; - io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; - io.KeyAlt = (mods & GLFW_MOD_ALT) != 0; + { + switch (key) + { + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: + io.KeyCtrl = false; + break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + io.KeyShift = false; + break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: + io.KeyAlt = false; + break; + default: + io.KeysDown[key] = false; + } + } } void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c) From fe157561294f7ac010dafea69eb1a312887a07e5 Mon Sep 17 00:00:00 2001 From: Dale Kim Date: Mon, 13 Apr 2015 20:04:53 -0500 Subject: [PATCH 2/2] Simplified glfw keyboard callbacks. --- .../opengl3_example/imgui_impl_glfw_gl3.cpp | 54 +++++-------------- examples/opengl_example/imgui_impl_glfw.cpp | 54 +++++-------------- 2 files changed, 28 insertions(+), 80 deletions(-) diff --git a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp index fa756500..0c18b32c 100644 --- a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp +++ b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp @@ -134,50 +134,24 @@ void ImGui_ImplGlfwGL3_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yo g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines. } -void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow*, int key, int, int action, int mods) +void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow* window, int key, int, int action, int mods) { ImGuiIO& io = ImGui::GetIO(); if (action == GLFW_PRESS) - { - switch (key) - { - case GLFW_KEY_LEFT_CONTROL: - case GLFW_KEY_RIGHT_CONTROL: - io.KeyCtrl = true; - break; - case GLFW_KEY_LEFT_SHIFT: - case GLFW_KEY_RIGHT_SHIFT: - io.KeyShift = true; - break; - case GLFW_KEY_LEFT_ALT: - case GLFW_KEY_RIGHT_ALT: - io.KeyAlt = true; - break; - default: - io.KeysDown[key] = true; - } - } - + io.KeysDown[key] = true; if (action == GLFW_RELEASE) - { - switch (key) - { - case GLFW_KEY_LEFT_CONTROL: - case GLFW_KEY_RIGHT_CONTROL: - io.KeyCtrl = false; - break; - case GLFW_KEY_LEFT_SHIFT: - case GLFW_KEY_RIGHT_SHIFT: - io.KeyShift = false; - break; - case GLFW_KEY_LEFT_ALT: - case GLFW_KEY_RIGHT_ALT: - io.KeyAlt = false; - break; - default: - io.KeysDown[key] = false; - } - } + io.KeysDown[key] = false; + + bool leftCtrl = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS; + bool rightCtrl = glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS; + bool leftShift = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; + bool rightShift = glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; + bool leftAlt = glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS; + bool rightAlt = glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS; + + io.KeyCtrl = leftCtrl || rightCtrl; + io.KeyShift = leftShift || rightShift; + io.KeyAlt = leftAlt || rightAlt; } void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow*, unsigned int c) diff --git a/examples/opengl_example/imgui_impl_glfw.cpp b/examples/opengl_example/imgui_impl_glfw.cpp index 3349a266..1e56ab13 100644 --- a/examples/opengl_example/imgui_impl_glfw.cpp +++ b/examples/opengl_example/imgui_impl_glfw.cpp @@ -115,50 +115,24 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffs g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines. } -void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods) +void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int, int action, int mods) { ImGuiIO& io = ImGui::GetIO(); if (action == GLFW_PRESS) - { - switch (key) - { - case GLFW_KEY_LEFT_CONTROL: - case GLFW_KEY_RIGHT_CONTROL: - io.KeyCtrl = true; - break; - case GLFW_KEY_LEFT_SHIFT: - case GLFW_KEY_RIGHT_SHIFT: - io.KeyShift = true; - break; - case GLFW_KEY_LEFT_ALT: - case GLFW_KEY_RIGHT_ALT: - io.KeyAlt = true; - break; - default: - io.KeysDown[key] = true; - } - } - + io.KeysDown[key] = true; if (action == GLFW_RELEASE) - { - switch (key) - { - case GLFW_KEY_LEFT_CONTROL: - case GLFW_KEY_RIGHT_CONTROL: - io.KeyCtrl = false; - break; - case GLFW_KEY_LEFT_SHIFT: - case GLFW_KEY_RIGHT_SHIFT: - io.KeyShift = false; - break; - case GLFW_KEY_LEFT_ALT: - case GLFW_KEY_RIGHT_ALT: - io.KeyAlt = false; - break; - default: - io.KeysDown[key] = false; - } - } + io.KeysDown[key] = false; + + bool leftCtrl = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS; + bool rightCtrl = glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS; + bool leftShift = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; + bool rightShift = glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; + bool leftAlt = glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS; + bool rightAlt = glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS; + + io.KeyCtrl = leftCtrl || rightCtrl; + io.KeyShift = leftShift || rightShift; + io.KeyAlt = leftAlt || rightAlt; } void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)