How to Enable Keyboard Navigation in Dear ImGui: Complete Implementation Guide

Enable keyboard navigation in Dear ImGui by setting the ImGuiConfigFlags_NavEnableKeyboard flag in the ConfigFlags field of the ImGuiIO structure immediately after creating the ImGui context.

The ocornut/imgui repository provides a single configuration flag that activates full keyboard accessibility across all widgets. When enabled, users can navigate interfaces using directional arrows, Tab/Shift-Tab cycling, and Space/Enter activation. This implementation detail is documented in the official FAQ and defined in the core header files of the library.

Locating the Configuration Flag

The ImGuiConfigFlags_NavEnableKeyboard flag is defined in imgui.h as part of the ImGuiConfigFlags_ enum. This bitmask instructs ImGui to process directional navigation inputs from the keyboard.

According to the source code, setting this flag activates full tabbing, directional-arrow movement, and Space/Enter activation for all interactive widgets. Without it, the library only provides basic tabbing and window switching capabilities.

Implementation Steps

To enable keyboard navigation in Dear ImGui, modify the ConfigFlags field immediately after creating the ImGui context but before the first frame renders.

  1. Retrieve the global IO object using ImGui::GetIO().
  2. Bitwise-OR the navigation flag into io.ConfigFlags.
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;

This initialization must occur after ImGui::CreateContext() returns but before your main loop begins calling ImGui::NewFrame().

Complete Setup Example

Here is a minimal implementation showing proper flag placement:

int main()
{
    // Create ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();

    // Enable keyboard navigation
    ImGuiIO& io = ImGui::GetIO();
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;

    // Initialize backend (GLFW, SDL, Win32, etc.) - omitted for brevity
    
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        ImGui::NewFrame();
        
        // Navigation-friendly widgets
        ImGui::Begin("Demo");
        ImGui::Button("Button A");      // Focusable via Tab/Arrows
        ImGui::Button("Button B");
        ImGui::Checkbox("Check me", &myFlag);
        ImGui::SliderFloat("Slider", &myValue, 0.0f, 1.0f);
        ImGui::End();
        
        ImGui::Render();
        // Render draw data - omitted
    }
    
    ImGui::DestroyContext();
    return 0;
}

Default Navigation Capabilities

Even when ImGuiConfigFlags_NavEnableKeyboard is not set, certain navigation features remain active. As documented in imgui_internal.h, basic tabbing via Tab and Shift+Tab along with window switching via Ctrl+Tab and Ctrl+Shift+Tab are always available regardless of the configuration flag. These default behaviors allow minimal keyboard interaction even in navigation-disabled modes.

Temporarily Disabling Navigation

You can toggle keyboard navigation at runtime by clearing the flag. This is useful when opening modal dialogs or entering text input modes where directional keys should control the cursor instead of UI focus.

if (ImGui::Button("Toggle Keyboard Nav"))
{
    ImGuiIO& io = ImGui::GetIO();
    if (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard)
        io.ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard; // Disable
    else
        io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable
}

The bitwise NOT operator (~) combined with bitwise AND (&=) safely removes only the navigation flag without affecting other configuration options like docking or viewports.

Summary

  • Set ImGuiConfigFlags_NavEnableKeyboard in io.ConfigFlags to enable full keyboard navigation in Dear ImGui
  • Initialize this flag immediately after ImGui::CreateContext() and before the first call to ImGui::NewFrame()
  • The flag is defined in imgui.h and controls directional arrows, Tab focus movement, and Space/Enter activation
  • Basic Tab and Ctrl+Tab navigation works even without the flag, as noted in imgui_internal.h
  • Toggle navigation at runtime using bitwise operations on io.ConfigFlags

Frequently Asked Questions

What is the minimum code required to enable keyboard navigation in Dear ImGui?

Retrieve the IO object with ImGuiIO& io = ImGui::GetIO(); then execute io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; immediately after creating the ImGui context. These two lines activate full directional navigation for all widgets in your interface.

Does Dear ImGui support keyboard navigation without enabling any flags?

Basic navigation works without flags. According to imgui_internal.h, Tab and Shift+Tab cycle through focusable items, and Ctrl+Tab switches between windows even when ImGuiConfigFlags_NavEnableKeyboard is disabled. However, directional arrow keys and Space/Enter activation require the explicit flag.

Where is the keyboard navigation flag defined in the source code?

The ImGuiConfigFlags_NavEnableKeyboard enum value is defined in imgui.h. The official documentation and usage examples appear in docs/FAQ.md, while the default initialization logic resides in imgui.cpp.

Can I enable keyboard navigation after the first frame has started?

Yes, you can modify io.ConfigFlags at any time during runtime. Changes take effect immediately, allowing you to dynamically toggle navigation modes based on application state, such as disabling navigation when a game camera needs exclusive control over arrow key input.

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 →