Demo: added "Shortcut Routing" section.
Also refer to e06bbe05e for what was reverted.
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index 5b273b4..cad6822 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -6480,6 +6480,77 @@
ImGui::TreePop();
}
+ // Demonstrate using Shortcut() and Routing Policies.
+ // The general flow is:
+ // - Code interested in a chord (e.g. "Ctrl+A") declares their intent.
+ // - Multiple locations may be interested in same chord! Routing helps find a winner.
+ // - Every frame, we resolve all claims and assign one owner if the modifiers are matching.
+ // - The lower-level function is 'bool SetShortcutRouting()', returns true when caller got the route.
+ // - Most of the times, SetShortcutRouting() is not called directly. User mostly calls Shortcut() with routing flags.
+ // - If you call Shortcut() WITHOUT any routing option, it uses ImGuiInputFlags_RouteFocused.
+ // TL;DR: Most uses will simply be:
+ // - Shortcut(ImGuiMod_Ctrl | ImGuiKey_A); // Use ImGuiInputFlags_RouteFocused policy.
+ IMGUI_DEMO_MARKER("Inputs & Focus/Shortcut Routing");
+ if (ImGui::TreeNode("Shortcut Routing"))
+ {
+ const float line_height = ImGui::GetTextLineHeightWithSpacing();
+ const ImGuiKeyChord key_chord = ImGuiMod_Ctrl | ImGuiKey_A;
+ static ImGuiInputFlags repeat_flags = ImGuiInputFlags_Repeat;
+ static ImGuiInputFlags routing_flags = ImGuiInputFlags_RouteFocused;
+ ImGui::CheckboxFlags("ImGuiInputFlags_Repeat", &repeat_flags, ImGuiInputFlags_Repeat);
+ ImGui::RadioButton("ImGuiInputFlags_RouteFocused (default)", &routing_flags, ImGuiInputFlags_RouteFocused);
+ ImGui::RadioButton("ImGuiInputFlags_RouteAlways", &routing_flags, ImGuiInputFlags_RouteAlways);
+ ImGui::RadioButton("ImGuiInputFlags_RouteGlobal", &routing_flags, ImGuiInputFlags_RouteGlobal);
+ ImGui::RadioButton("ImGuiInputFlags_RouteGlobalHigh", &routing_flags, ImGuiInputFlags_RouteGlobalHigh);
+ ImGui::RadioButton("ImGuiInputFlags_RouteGlobalLow", &routing_flags, ImGuiInputFlags_RouteGlobalLow);
+ const ImGuiInputFlags flags = repeat_flags | routing_flags; // Merged flags
+
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, 0, flags) ? "PRESSED" : "...");
+
+ ImGui::BeginChild("WindowA", ImVec2(-FLT_MIN, line_height * 18), true);
+ ImGui::Text("Press CTRL+A and see who receives it!");
+ ImGui::Separator();
+
+ // 1: Window polling for CTRL+A
+ ImGui::Text("(in WindowA)");
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, 0, flags) ? "PRESSED" : "...");
+
+ // 2: InputText also polling for CTRL+A: it always uses _RouteFocused internally (gets priority when active)
+ char str[16] = "Press CTRL+A";
+ ImGui::Spacing();
+ ImGui::InputText("InputTextB", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly);
+ ImGuiID item_id = ImGui::GetItemID();
+ ImGui::SameLine(); HelpMarker("Internal widgets always use _RouteFocused");
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, item_id, flags) ? "PRESSED" : "...");
+
+ // 3: Dummy child is not claiming the route: focusing them shouldn't steal route away from WindowA
+ ImGui::BeginChild("ChildD", ImVec2(-FLT_MIN, line_height * 4), true);
+ ImGui::Text("(in ChildD: not using same Shortcut)");
+ ImGui::Text("IsWindowFocused: %d", ImGui::IsWindowFocused());
+ ImGui::EndChild();
+
+ // 4: Child window polling for CTRL+A. It is deeper than WindowA and gets priority when focused.
+ ImGui::BeginChild("ChildE", ImVec2(-FLT_MIN, line_height * 4), true);
+ ImGui::Text("(in ChildE: using same Shortcut)");
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, 0, flags) ? "PRESSED" : "...");
+ ImGui::EndChild();
+
+ // 5: In a popup
+ if (ImGui::Button("Open Popup"))
+ ImGui::OpenPopup("PopupF");
+ if (ImGui::BeginPopup("PopupF"))
+ {
+ ImGui::Text("(in PopupF)");
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, 0, flags) ? "PRESSED" : "...");
+ ImGui::InputText("InputTextG", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly);
+ ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, ImGui::GetItemID(), flags) ? "PRESSED" : "...");
+ ImGui::EndPopup();
+ }
+ ImGui::EndChild();
+
+ ImGui::TreePop();
+ }
+
// Display mouse cursors
IMGUI_DEMO_MARKER("Inputs & Focus/Mouse Cursors");
if (ImGui::TreeNode("Mouse Cursors"))