improve imgui modifier key handling:

This commit is contained in:
lauchmelder 2025-01-05 21:02:32 +01:00
parent 2b0f757fcb
commit 1943b5be64
3 changed files with 22 additions and 24 deletions

View file

@ -4,5 +4,5 @@
namespace qk { namespace qk {
ImGuiMouseButton TranslateMouseButton(int button); ImGuiMouseButton TranslateMouseButton(int button);
int TranslateKey(int key); ImGuiKey TranslateKey(int key);
} }

View file

@ -12,7 +12,7 @@ namespace qk {
return button; return button;
} }
int TranslateKey(int key) { ImGuiKey TranslateKey(int key) {
switch (key) { switch (key) {
case GLFW_KEY_TAB: return ImGuiKey_Tab; case GLFW_KEY_TAB: return ImGuiKey_Tab;
case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
@ -29,14 +29,14 @@ namespace qk {
case GLFW_KEY_SPACE: return ImGuiKey_Space; case GLFW_KEY_SPACE: return ImGuiKey_Space;
case GLFW_KEY_ENTER: return ImGuiKey_Enter; case GLFW_KEY_ENTER: return ImGuiKey_Enter;
case GLFW_KEY_ESCAPE: return ImGuiKey_Escape; case GLFW_KEY_ESCAPE: return ImGuiKey_Escape;
case GLFW_KEY_LEFT_CONTROL: return (ImGuiKey)(ImGuiKey_LeftCtrl | ImGuiMod_Ctrl); case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl;
case GLFW_KEY_LEFT_SHIFT: return (ImGuiKey)(ImGuiKey_LeftShift | ImGuiMod_Shift); case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift;
case GLFW_KEY_LEFT_ALT: return (ImGuiKey)(ImGuiKey_LeftAlt | ImGuiMod_Alt); case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt;
case GLFW_KEY_LEFT_SUPER: return (ImGuiKey)(ImGuiKey_LeftSuper | ImGuiMod_Super); case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper;
case GLFW_KEY_RIGHT_CONTROL: return (ImGuiKey)(ImGuiKey_RightCtrl | ImGuiMod_Ctrl); case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl;
case GLFW_KEY_RIGHT_SHIFT: return (ImGuiKey)(ImGuiKey_RightShift | ImGuiMod_Shift); case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift;
case GLFW_KEY_RIGHT_ALT: return (ImGuiKey)(ImGuiKey_RightAlt | ImGuiMod_Alt); case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt;
case GLFW_KEY_RIGHT_SUPER: return (ImGuiKey)(ImGuiKey_RightSuper | ImGuiMod_Super); case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper;
case GLFW_KEY_MENU: return ImGuiKey_Menu; case GLFW_KEY_MENU: return ImGuiKey_Menu;
case GLFW_KEY_0: return ImGuiKey_0; case GLFW_KEY_0: return ImGuiKey_0;
case GLFW_KEY_1: return ImGuiKey_1; case GLFW_KEY_1: return ImGuiKey_1;

View file

@ -107,52 +107,50 @@ namespace qk {
bool ImGuiLayer::OnKeyPressedEvent(KeyPressedEvent& e) { bool ImGuiLayer::OnKeyPressedEvent(KeyPressedEvent& e) {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
int key = TranslateKey(e.GetKeyCode()); ImGuiKey key = TranslateKey(e.GetKeyCode());
int mod = ImGuiMod_Mask_ & key;
if ((mod & ImGuiMod_Ctrl) != 0) { if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) {
io.AddKeyEvent(ImGuiMod_Ctrl, true); io.AddKeyEvent(ImGuiMod_Ctrl, true);
} }
if ((mod & ImGuiMod_Shift) != 0) { if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) {
io.AddKeyEvent(ImGuiMod_Shift, true); io.AddKeyEvent(ImGuiMod_Shift, true);
} }
if ((mod & ImGuiMod_Alt) != 0) { if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) {
io.AddKeyEvent(ImGuiMod_Alt, true); io.AddKeyEvent(ImGuiMod_Alt, true);
} }
if ((mod & ImGuiMod_Super) != 0) { if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) {
io.AddKeyEvent(ImGuiMod_Super, true); io.AddKeyEvent(ImGuiMod_Super, true);
} }
io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), true); io.AddKeyEvent(key, true);
return false; return false;
} }
bool ImGuiLayer::OnKeyReleasedEvent(KeyReleasedEvent& e) { bool ImGuiLayer::OnKeyReleasedEvent(KeyReleasedEvent& e) {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
int key = TranslateKey(e.GetKeyCode()); ImGuiKey key = TranslateKey(e.GetKeyCode());
int mod = ImGuiMod_Mask_ & key;
if ((mod & ImGuiMod_Ctrl) != 0) { if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) {
io.AddKeyEvent(ImGuiMod_Ctrl, false); io.AddKeyEvent(ImGuiMod_Ctrl, false);
} }
if ((mod & ImGuiMod_Shift) != 0) { if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) {
io.AddKeyEvent(ImGuiMod_Shift, false); io.AddKeyEvent(ImGuiMod_Shift, false);
} }
if ((mod & ImGuiMod_Alt) != 0) { if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) {
io.AddKeyEvent(ImGuiMod_Alt, false); io.AddKeyEvent(ImGuiMod_Alt, false);
} }
if ((mod & ImGuiMod_Super) != 0) { if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) {
io.AddKeyEvent(ImGuiMod_Super, false); io.AddKeyEvent(ImGuiMod_Super, false);
} }
io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), false); io.AddKeyEvent(key, false);
return false; return false;
} }