From fe157561294f7ac010dafea69eb1a312887a07e5 Mon Sep 17 00:00:00 2001 From: Dale Kim Date: Mon, 13 Apr 2015 20:04:53 -0500 Subject: [PATCH] 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 fa756500b..0c18b32c7 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 3349a2664..1e56ab139 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)