Skip to content

InputInt and IsItemDeactivatedAfterEdit don't work as expected when pressing Escape #8915

Closed

Description

@tom-seddon

Version/Branch of Dear ImGui:

v1.92.2b-docking (1f7f1f5)

Back-ends:

probably all, but tested with win32_directx9

Compiler, OS:

probably all, but using Windows 10 (Version 22H2 (OS Build 19045.6216)) and VS2022 (Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35215 for x64)

Full config/build information:

Dear ImGui 1.92.2b (19222)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx9
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C1F
 HasGamepad
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------
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:

I'm trying to make InputInt work when there's no direct access to the actual value - just by-value getters/setters. (It'd be inconvenient to change this.)

The code I'd expect to be able to use would be along these lines:

int tmp=GetValue();
if(ImGui::InputInt("label",&tmp)) {
    if(ImGui::IsItemDeactivatedAfterEdit()) {
        SetValue(tmp);
    }
}

And this does work somewhat ok, in that SetValue is called for pressing Return, but not for intermediate edits, as desired - but, after pressing Escape, IsItemDeactivatedAfterEdit( returns true (which is fine), and tmp is the edited value rather than the original one (which is a bit surprising).

I don't mind if SetValue gets called again with the original value, because the setter can always check and do nothing if it's the same value. But it feels odd that pressing Escape doesn't undo the edits! The behaviour of the input int widget in the Widgets/Basic/InputText section of the demo is much more as I'd expect: Return accepts the edited value, and Escape reverts its it to the pre-edit value. It operates directly on a static int though which isn't appropriate in this case.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

You can clone my test code from a branch of my fork:

git clone https://github.com/tom-seddon/imgui
cd imgui
git checkout tom/InputInt_problem

The changes are to imgui_demo.cpp (diff here: tom-seddon@b6273fe) - so it should hopefully apply to any example with any backends with any OS/compiler combination, but I've been building and running example_win32_directx9 from the VS2022 IDE on Windows 10 (exact versions as above)

Repro steps:

  1. Build and run demo
  2. Expand the Widgets/Basic section in the demo. There should be a row labelled input int (2) visible - this is the test I've added
  3. Click on the input int row, current value 123. Type in 124 and press Return. The value changes to 124
  4. Click on the input int row, current value 124. Type in 125 and press Escape. The value changes back to 124
  5. Click on the input int (2) row, current value 124. Type in 125 and press Return. The value changes to 125
  6. Click on the input int (2) row, current value 125. Type in 126 and press Escape. The value changes to 126 rather than reverting to 125

Activity

  1. ocornut commented on Sep 4, 2025

    @ocornut
    Owner

    Thanks for your report.

    FYI you don't need to put effort making a patch for such a simple repro (this is more work for you but actually also for me)

    The changes are to imgui_demo.cpp (diff here: tom-seddon@b6273fe) - so it should hopefully apply to any example with any backends with any OS/compiler combination, but I've been building and running example_win32_directx9 from the VS2022 IDE on Windows 10 (exact versions as above)

    The repro is simply the block I can paste anywhere to confirm and test the issue:

    static int s_value = 0;
    int tmp = s_value;
    if (ImGui::InputInt("label", &tmp))
        if (ImGui::IsItemDeactivatedAfterEdit())
            s_value = tmp;
  2. ocornut commented on Sep 4, 2025

    @ocornut
    Owner

    It is caused by this block:

            if (revert_edit && !is_readonly)
            {
                [...] else if (strcmp(buf, state->TextToRevertTo.Data) != 0)
                {
                    apply_new_text = state->TextToRevertTo.Data;
                    apply_new_text_length = state->TextToRevertTo.Size - 1;

    The compare is done on the input buffer rather that the edit buffer, therefore the buffers matches and the value is not applied.

    Interestingly that check has been here since the dawn of time, but it didn't trigger the bug until 1.91.7 because of 00f12b9 (#8273)

  3. ocornut commented on Sep 4, 2025

    @ocornut
    Owner

    This seems to pass all tests and is consistent with the fact that we don't use/rely on buf in InputTextEx() for most operations.

  4. tom-seddon commented on Sep 4, 2025

    @tom-seddon
    ContributorAuthor

    Now running with a959617 in my project and it's working nicely. Thanks ever so much for the quick turnaround.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址