From c59ebb2d715417dec2e7799fc97739e1101558a8 Mon Sep 17 00:00:00 2001 From: ocornut Date: Tue, 5 Jul 2022 14:48:55 +0200 Subject: [PATCH] Inputs: added basic Shortcut() function - no routing yet. (#456) --- imgui.cpp | 20 ++++++++++++++++++++ imgui_internal.h | 11 +++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b469da136..beefbf747 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -8420,6 +8420,26 @@ void ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags) SetKeyOwner(key, id, flags); } +// - Need to decide how to handle shortcut translations for Non-Mac <> Mac +// - Ideas: https://github.com/ocornut/imgui/issues/456#issuecomment-264390864 +bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); + if (g.IO.KeyMods != mods) + return false; + + // Special storage location for mods + if (key == ImGuiKey_None) + key = ConvertSingleModFlagToKey(mods); + + if (!IsKeyPressed(key, owner_id, (flags & (ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_)))) + return false; + return true; +} + + //----------------------------------------------------------------------------- // [SECTION] ERROR CHECKING //----------------------------------------------------------------------------- diff --git a/imgui_internal.h b/imgui_internal.h index 39b8aa32c..d863efb0c 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1289,11 +1289,11 @@ struct ImGuiKeyOwnerData ImGuiKeyOwnerData() { OwnerCurr = OwnerNext = ImGuiKeyOwner_None; LockThisFrame = LockUntilRelease = false; } }; -// Flags for extended versions of IsKeyPressed(), IsMouseClicked(), SetKeyOwner(), SetItemKeyOwner() +// Flags for extended versions of IsKeyPressed(), IsMouseClicked(), Shortcut(), SetKeyOwner(), SetItemKeyOwner() // Don't mistake with ImGuiInputTextFlags! (for ImGui::InputText() function) enum ImGuiInputFlags_ { - // Flags for IsKeyPressed(), IsMouseClicked() + // Flags for IsKeyPressed(), IsMouseClicked(), Shortcut() ImGuiInputFlags_None = 0, ImGuiInputFlags_Repeat = 1 << 0, // Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) @@ -2817,6 +2817,13 @@ namespace ImGui IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags = 0); IMGUI_API bool IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id); + // [EXPERIMENTAL] Shortcuts + // - ImGuiKeyChord = any ImGuiKey optionally ORed with ImGuiMod_XXX values. + // ImGuiKey_C (accepted by functions taking ImGuiKey or ImGuiKeyChord) + // ImGuiKey_C | ImGuiMod_Ctrl (accepted by functions taking ImGuiKeyChord) + // - ONLY ImGuiMod_XXX values are legal to 'OR' with an ImGuiKey. You CANNOT 'OR' two ImGuiKey values. + IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); + // [EXPERIMENTAL] Focus Scope // This is generally used to identify a unique input location (for e.g. a selection set) // There is one per window (automatically set in Begin), but: