Skip to content

Recommended way to expand / collapse tree nodes with keyboard shortcuts? #7553

@sodamouse

Description

@sodamouse

Version/Branch of Dear ImGui:

Version 1.89.8, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Windows 11 + MSVC 2022

Full config/build information:

No response

Details:

My Issue/Question:

Hi, I am trying to figure out the best way to control expanding / collapsing a tree node using a keyboard shortcut.
Currently, I use a simple bool flag + ImGuiTreeNodeFlag_DefaultOpen the to do this (see example).

I know this is very hacky, but it works fine until I start to also use the mouse to expand / collapse the node, after which the keyboard shortcut stops working. I'm sure that there is a recommended way to do this already, but I did not manage to find it.

Any suggestions?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

    ImGuiTreeNodeFlags filterNodeFlags  = 0;

    if (filterNodeOpen) filterNodeFlags = ImGuiTreeNodeFlags_DefaultOpen;

    if (ImGui::TreeNodeEx("Filter", filterNodeFlags))
    {
        // ...
        ImGui::TreePop();
    }
// somewhere else in code
    if (!showTagsPopup && ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && ImGui::IsKeyPressed(ImGuiKey_G)) filterNodeOpen = !filterNodeOpen;

Metadata

Metadata

Assignees

No one assigned

    Labels

    treetree nodes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions