How to Enable Keyboard and Gamepad Navigation in Dear ImGui: Configuring NavEnableKeyboard and NavEnableGamepad

Set ImGuiConfigFlags_NavEnableKeyboard and ImGuiConfigFlags_NavEnableGamepad in ImGuiIO::ConfigFlags during initialization to activate full keyboard and gamepad control, with gamepad additionally requiring ImGuiBackendFlags_HasGamepad from your backend.

Dear ImGui provides a built-in navigation system that allows users to interact with the entire UI without a mouse. By configuring two flags in imgui.h, you can enable complete keyboard and gamepad navigation across all widgets in the ocornut/imgui repository. This guide covers the exact initialization code and backend requirements needed to implement these features.

Understanding the Navigation Configuration Flags

In imgui.h (lines 1713-1714), the ImGuiConfigFlags_ enum defines two critical flags that control input navigation:

  • ImGuiConfigFlags_NavEnableKeyboard: Enables directional navigation using arrow keys, focus management, and activation via Space/Enter.
  • ImGuiConfigFlags_NavEnableGamepad: Enables gamepad navigation via D-pad, analog sticks, and controller buttons.

Both flags are disabled by default. You apply them to the ImGuiIO::ConfigFlags bitmask immediately after calling ImGui::CreateContext().

Enabling Navigation in Your Application

Add the following initialization code after creating your ImGui context:

// Create context and get IO reference
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();

// Enable navigation flags
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;   // Enable keyboard navigation
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;    // Enable gamepad navigation

This setup typically occurs before initializing platform backends like ImGui_ImplGlfw_InitForOpenGL. Once set, ImGui automatically handles focus movement and widget activation for all standard UI elements.

Backend Requirements for Gamepad Support

While keyboard navigation activates immediately once the flag is set, gamepad navigation requires backend cooperation. The platform backend must report ImGuiBackendFlags_HasGamepad in io.BackendFlags when a controller is connected.

According to the comments in imgui_impl_glfw.h (line 10), the GLFW backend automatically handles this detection. Other backends in the backends/ directory (SDL2, Vulkan, etc.) implement similar detection logic.

You can verify gamepad availability at runtime:

if (io.BackendFlags & ImGuiBackendFlags_HasGamepad) {
    // Gamepad is connected and active
}

Once enabled, the navigation system in ocornut/imgui provides:

  1. Directional movement: Arrow keys, WASD, D-pad, or left analog stick to move focus between widgets.
  2. Widget activation: Space, Enter, or the gamepad "A" button to activate buttons, sliders, and other controls.
  3. Tab cycling: Tab and Shift+Tab to move focus forward and backward through focusable items.
  4. Window management: Ctrl+Tab and Ctrl+Shift+Tab to switch between ImGui windows (this works regardless of navigation flags, as noted in imgui_internal.h line 2440).

Fine-Tuning Navigation Settings

You can adjust navigation dynamics through additional ImGuiIO members:

Navigation Speed:

io.NavMoveSpeed = 2.0f;  // Default is 1.0f; higher values move focus faster

Input Access: The NavInputs array in ImGuiIO allows you to query raw navigation input values if you need custom handling, though the standard mapping works for most applications.

Summary

  • Set ImGuiConfigFlags_NavEnableKeyboard and ImGuiConfigFlags_NavEnableGamepad in io.ConfigFlags to enable full navigation.
  • Gamepad support requires the backend to set ImGuiBackendFlags_HasGamepad.
  • These flags are defined in imgui.h and control the focus system implemented throughout the library.
  • Basic tabbing and window switching work regardless of flag settings, but full directional navigation requires the flags.

Frequently Asked Questions

Does keyboard navigation work without the NavEnableKeyboard flag?

Basic tabbing with Tab and Shift+Tab is always available, but full directional navigation using arrow keys and activation keys requires the ImGuiConfigFlags_NavEnableKeyboard flag to be set.

Why isn't my gamepad responding even with NavEnableGamepad enabled?

The backend must report ImGuiBackendFlags_HasGamepad in io.BackendFlags. Check that your platform backend (e.g., GLFW, SDL2) supports gamepad detection and that a controller is actually connected to the system.

Can I customize the navigation speed or key mappings?

Yes. Adjust io.NavMoveSpeed for faster or slower focus movement. For custom key mappings, you can modify the NavInputs array in ImGuiIO before calling NewFrame(), though this is rarely necessary for standard applications.

Do I need to add special code to each widget to support navigation?

No. Once the flags are enabled, all standard ImGui widgets (buttons, sliders, menus, etc.) automatically support navigation. The system handles focus management and activation internally.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →