Skip to content

Inconsistent IsItemActivated in an ImGuiInputTextCallback between InputText and InputTextMultiline #9174

Closed

Description

@abeug

Version/Branch of Dear ImGui:

Version v1.92.5, Branch: docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx12.cpp

Compiler, OS:

MSVC 2026, Windows 11

Full config/build information:

Dear ImGui 1.92.5 (19250)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1950
define: _MSVC_LANG=201402
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000001E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
 RendererHasTextures
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

My Issue/Question:

I am seeing a difference between when InputText and InputTextMultiline are activated inside an ImGuiInputTextCallback when I explicitly give them keyboard focus with SetKeyboardFocusHere(). Calling IsItemActivated() inside of an always executing callback returns true on first execution of the callback when using an InputText. However, if an InputTextMultiline is used instead, the item is never activated inside of the callback.

Screenshots/Video:

Example: Here is an example with a rename Button and a filename as disabled text.
Image

Using InputText
If rename is clicked, an InputText is displayed to allow renaming of the file. The InputText is given keyboard focus and the filename is selected on first activation using a callback. This works when InputText is used.
Image

Using InputTextMultiline
However, if an InputTextMultiline is used instead (see the checkbox), the text does not get selected. The cursor/caret remains in the default position. This happens becauses IsItemActivated() returns false in the callback, so the selection code never runs.
Image

I would expect that they have the same behavior

Minimal, Complete and Verifiable Example code:

Here is the repro code for the above problem.

Callback Function

static int selectFileNameOnActivate(ImGuiInputTextCallbackData* data)
{
    if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways)
    {
        if (ImGui::IsItemActivated()) // Not triggered when using InputTextMultiline
        {
            const char* dot = strrchr(data->Buf, '.');
            if (dot != nullptr)
            {
                int offset = dot - data->Buf;
                data->SelectionStart = 0;
                data->SelectionEnd = offset;
                data->CursorPos = offset;
            }
            else
            {
                data->SelectAll();
            }
        }
    }
    return 0;
};

Main Code

static bool useMultiline = false;
ImGui::Checkbox("Use MultiLine", &useMultiline);

static bool isRenameActive = false;
if (ImGui::Button("Rename (Should select the filename)"))
{
    isRenameActive = true;
    ImGui::SetKeyboardFocusHere();
}

const int bufferSize = 1024;
static char buffer[bufferSize] = "ExampleFileName.extension";
if (isRenameActive)
{
    ImGuiInputTextFlags flags = ImGuiInputTextFlags_EnterReturnsTrue
        | ImGuiInputTextFlags_CtrlEnterForNewLine
        | ImGuiInputTextFlags_CallbackAlways;

    bool renameConfirmed = false;
    if (useMultiline)
        renameConfirmed = ImGui::InputTextMultiline("##renameBox", buffer, bufferSize, ImVec2(300, 50), flags, selectFileNameOnActivate);
    else
        renameConfirmed = ImGui::InputText("##renameBox", buffer, bufferSize, flags, selectFileNameOnActivate);

    if (renameConfirmed || ImGui::IsItemDeactivated())
        isRenameActive = false;
}
else
{
    ImGui::TextDisabled(buffer);
}

Activity

  1. ocornut commented on Jan 21, 2026

    @ocornut
    Owner

    Hello,

    Calling IsItemActivated() inside of an always executing callback

    This is unreliable because at the time of the callback the item has not completed.

    I've pushed four commits 42b7704, f21307e, d448045, cb3b7ff which should give you access to alternatives.
    I'll document them all here to increase discoverability but you would only need one.

    (A) ImGuiInputTextCallbackData now exposes a EventActivated field. So you can do:

    static int selectFileNameOnActivate(ImGuiInputTextCallbackData* data)
    {
        if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways)
        {
            if (data->EventActivated)
            {
                const char* dot = strrchr(data->Buf, '.');
                if (dot != nullptr)
                    data->SetSelection(0, (int)(dot - data->Buf));
                else
                    data->SelectAll();
            }
        }
        return 0;
    };

    Technically I could make a small change to ensure that IsItemActivated() would work during the callback but I'm reluctant at this would be rather fragile ground and not all other IsItemXXX functions will work.

    (B) ImGuiInputTextCallbackData now also exposes the item ID, which generally may allow for variety of things including directly accessing the ImGuiInputTextState struct from the callback.
    Technically you could also hack a if (ImGui::GetActiveID() == data->ID && ImGui::GetCurrentContext()->ActiveIdIsJustActivated) check but here you don't need it as (A) is simpler and now public API.

    (C) You can also access the internal state:

    #include "imgui_internal.h"
    [...]
    if (useMultiline)
        renameConfirmed = ImGui::InputTextMultiline("##renameBox", buffer, bufferSize, ImVec2(300, 50), flags, selectFileNameOnActivate);
    else
        renameConfirmed = ImGui::InputText("##renameBox", buffer, bufferSize, flags, selectFileNameOnActivate);
    
    bool activated = ImGui::IsItemActivated();
    bool deactivated = ImGui::IsItemDeactivated();
    if (activated)
        if (ImGuiInputTextState* state = ImGui::GetInputTextState(ImGui::GetItemID()))
            if (const char* dot = strrchr(state->TextSrc, '.'))
                state->SetSelection(0, (int)(dot - state->TextSrc));

    You'd need to update either way: the ImGuiInputTextState version of SetSelection() somehow was missing before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址