Skip to main content

Overview

The shortcut system allows multiple widgets to register interest in a shortcut, but only one owner gets it based on focus routing. This makes shortcuts work correctly in complex hierarchies.
Use Shortcut() instead of IsKeyPressed()/IsKeyChordPressed() for better features and focus routing support.

Key Concepts

The general idea:
  • Several callers may register interest in a shortcut
  • Only one owner gets it based on focus routing
  • The system is order independent

Shortcut Function

Shortcut

Test if a keyboard shortcut is pressed and routed to the calling code.
ImGuiKeyChord
Key chord (e.g., ImGuiMod_Ctrl | ImGuiKey_S)
ImGuiInputFlags
default:"0"
Input flags for routing and repeat behavior
bool
True if shortcut was pressed and routed to this code
Example:

SetNextItemShortcut

Set a keyboard shortcut for the next item. The shortcut will be displayed in tooltips.
ImGuiKeyChord
Key chord for the shortcut
ImGuiInputFlags
default:"0"
Input flags
Example:

Key Chords

A key chord is a key optionally combined with modifiers:
Only ImGuiMod_XXX values are legal to combine with an ImGuiKey. You CANNOT combine two ImGuiKey values.

Input Flags

Repeat Flag

Example:

Routing Policies

Default policy is ImGuiInputFlags_RouteFocused. Can select only 1 policy:
ImGuiInputFlags
Route to active item only.
ImGuiInputFlags
Route to windows in the focus stack (DEFAULT). Deep-most focused window takes inputs.
ImGuiInputFlags
Global route (unless a focused window or active item registered the route).
ImGuiInputFlags
Do not register route, poll keys directly.

Routing Options

Example with routing:

Tooltip Flag

Example:

Routing Priority

Routing policies are resolved in this order:
  1. RouteGlobal + OverActive
  2. RouteActive or RouteFocused (if owner is active item)
  3. RouteGlobal + OverFocused
  4. RouteFocused (if in focused window stack)
  5. RouteGlobal

Key Ownership

SetItemKeyOwner

Set key owner to last item ID if it is hovered or active. Equivalent to:
ImGuiKey
Key to claim ownership of
Use case: Disable standard input behaviors like mouse wheel scrolling. Example:

Difference: IsKeyChordPressed vs Shortcut

IsKeyChordPressed

  • Compares mods and calls IsKeyPressed()
  • No side-effects
  • No routing

Shortcut

  • Submits a route
  • Routes are resolved
  • Calls IsKeyChordPressed() if routed
  • Has side-effects (can prevent another call from getting the route)

Common Shortcut Patterns

Standard Editor Shortcuts

Application-Wide Shortcuts

Context-Sensitive Shortcuts

Debugging

Visualize registered routes in Metrics/Debugger -> Inputs.

Platform Considerations

On macOS, Dear ImGui automatically swaps Cmd(Super) and Ctrl keys:
  • ImGuiMod_Ctrl = Cmd on macOS, Ctrl on other platforms
  • ImGuiMod_Super = Ctrl on macOS, Windows/Super on other platforms

See Also

  • Keyboard - Keyboard input functions
  • Mouse - Mouse input functions
  • Input - General input functions