Input Routing Philosophy
Dear ImGui’s input system is designed around a crucial principle:Always forward ALL input to Dear ImGui, then check if ImGui wants to use it before passing it to your application.
- Detect clicks in empty space to unfocus windows
- Handle dragging that starts outside widgets
- Properly manage focus and hover states
- Provide smooth interaction behavior
The Want Capture Flags
After callingImGui::NewFrame(), check these flags in ImGuiIO to determine input routing:
io.WantCaptureMouse
Set when Dear ImGui wants to use mouse input:- Hovering over any Dear ImGui window
- Clicking on any Dear ImGui widget
- Dragging a slider, scrollbar, or window
- Using mouse wheel over a Dear ImGui window
io.WantCaptureKeyboard
Set when Dear ImGui wants to use keyboard input:- An
InputTextwidget has focus - Keyboard navigation is active (
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard) - A keyboard shortcut is being processed
Special case: Text input widgets release focus on the “KeyDown” event of Return, so the “KeyUp” event will have
io.WantCaptureKeyboard == false. Track which keys were pressed while ImGui had focus if this matters to your application.io.WantTextInput
Set when Dear ImGui expects text input (useful for on-screen keyboards):- An
InputTextwidget is active - An
InputTextMultilinewidget is active
Mouse Input
Mouse Position
Mouse Buttons
Mouse Wheel
Mouse Source (Optional)
Reading Mouse State
Keyboard Input
Key Events (Modern API)
Dear ImGui uses a named key system:Character Input
For text input, send Unicode characters:On Windows, use
WM_CHAR messages. On other platforms, convert keyboard input to UTF-8/UTF-16 characters.Reading Keyboard State
Keyboard Shortcuts
Modifier Keys
Gamepad Input
Enable gamepad navigation:Gamepad Buttons
Gamepad Analog Inputs
You’re responsible for applying dead zones and normalizing analog values. ImGui expects values in the range 0.0 to 1.0.
Focus and Hover Detection
Window Focus
Window Hover
Item Hover and Focus
Custom Input Handling Example
Here’s a complete example of routing input:IME (Input Method Editor) Support
For languages like Chinese, Japanese, Korean:Touch Input
Touch input can be mapped to mouse input:Common Patterns
Pausing Game When UI is Active
Global Shortcuts
Mouse Delta for Camera
Next Steps
- Explore Widgets to see how input is used in practice
- Learn about Best Practices
- Check out Backend Overview for platform-specific input handling