From 56b37bf93c3cbead5f2d4d4369848415a3b0a8b2 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 7 May 2026 21:22:02 +0200 Subject: [PATCH] Inputs: SetItemKeyOwner(): does not set ownership is key is already taken. (#456, #2637, #2620, #2891, #3370, #3724, #4828, #5108, #5242, #5641) --- docs/CHANGELOG.txt | 4 ++++ imgui.cpp | 2 ++ imgui_demo.cpp | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index e8f4776b7..a35a7de07 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -156,6 +156,10 @@ Other Changes: - SetItemKeyOwner(): return true if ownership has been requested, which typically needs to to checked for gating further tests. This is important as the function may fail. (#456, #2637, #2620, #2891, #3370, #3724, #4828, #5108, #5242, #5641) + - SetItemKeyOwner(): does not set ownership is key is already taken. Effectively + this makes using `SetItemKeyOwner(ImGuiKey_MouseWheelY)` over an item work as + expected while not having item react if a scroll wheel is actively in progress. + May be subject to further redesign, e.g. conditional flags. Feedback welcome! - Style: - Fixed vertical scrollbar top coordinates when using thick borders on windows with no title bar and no menu bar. (#9366) diff --git a/imgui.cpp b/imgui.cpp index cd21c2735..8615cc0c6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -10726,6 +10726,8 @@ bool ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags) if ((g.HoveredId == id && (flags & ImGuiInputFlags_CondHovered)) || (g.ActiveId == id && (flags & ImGuiInputFlags_CondActive))) { IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetItemKeyOwner) == 0); // Passing flags not supported by this function! + if (!TestKeyOwner(key, id)) + return false; SetKeyOwner(key, id, flags & ~ImGuiInputFlags_CondMask_); return true; } diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 39d9e1f07..d251616ea 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -875,7 +875,7 @@ static void ExampleImageViewer_DrawCanvas(ExampleImageViewerData* data, ImVec2 c data->ViewReset = false; // Handle inputs - if (ImGui::SetItemKeyOwner(ImGuiKey_MouseWheelY)) // FIXME: Not while scrolling? + if (ImGui::SetItemKeyOwner(ImGuiKey_MouseWheelY)) if (io.MouseWheel != 0.0f) data->Zoom = IM_CLAMP(data->Zoom * (1.0f + io.MouseWheel * 0.10f), data->ZoomMin, data->ZoomMax); float zoom = data->Zoom; // (float)(int)ViewZoom;