Handle input dispatching between ImGui and your application
masterTo prevent input conflicts (e.g., clicking a button in ImGui also triggering a game action), use the flags provided in the ImGuiIO structure.
io.WantCaptureMouse: Set when ImGui wants to consume mouse input. You should discard/hide mouse inputs from your application when this is true.io.WantCaptureKeyboard: Set when ImGui wants to consume keyboard input. You should discard/hide keyboard inputs from your application when this is true.io.WantTextInput: Set when ImGui needs text input (e.g., an active text box). You may want to trigger an OS on-screen keyboard.
Important Implementation Details:
- Always pass inputs to ImGui: Even if the
WantCaptureflags arefalse, you must still pass mouse/keyboard events to ImGui so it can detect clicks in empty space to unfocus windows. - Timing: These flags are updated by
ImGui::NewFrame(). For best results, read them after callingNewFrame(). - Accuracy: Do not manually check if the mouse is hovering an ImGui window;
io.WantCaptureMouseis the authoritative source and correctly handles dragging and modal windows. - Keyboard Edge Case: Text input widgets release focus on "Return KeyDown". This means the subsequent "Return KeyUp" event might arrive when
io.WantCaptureKeyboardis alreadyfalse. You may need to track key-down states to filter these out.
if (ImGui::GetIO().WantCaptureMouse) {
// Discard mouse input for the rest of the application
}