Keep keys away from the 3D view while ImGui has the keyboard (#16673)

The 3D view decided whether to run its shortcuts from ImGui's "wants the
keyboard" flag, which is updated a frame late. The first key typed after
clicking into a field could still reach the canvas.

The canvas now leaves every key to ImGui while an ImGui text field is
active, while a popup (dropdown, menu or modal) is open, or while a
slider or button is held with the mouse. A focused sidebar text field
works the same way. Global shortcuts still run unless they edit text.
Every key still goes to ImGui.

- ImGuiWrapper::claim_key_event() decides on the key-down. The key's
  repeats, char event and release follow that decision, so a key never
  both runs a shortcut and types.
- Esc closes an open dropdown or menu before the gizmo.
- Esc and Tab during a slider drag no longer close the gizmo or switch
  views.
- text_input_active() reads ImGui's text input request, so a slider
  that once had a value typed into it no longer counts as a text field
  when dragged.
- A modifier whose key-up went to a dialog no longer stays stuck. The
  canvas releases held modifiers when it loses the focus, and ImGui
  takes its modifiers from each mouse event.
- The macOS Edit menu's key equivalents follow the same rule.
This commit is contained in:
Kris Austin authored and GitHub committed 2026-10-22 10:22:23 -05:00
1 parent f92f75e0fa
commit 3f8ff53e65
8 files changed
+343 -37

No files matched your search

+192 -1
View File
@@ -75,7 +75,27 @@ struct ImGuiFrames
ImGui::End();
}
char text[16] = "";
void draw_slider()
{
ImGui::Begin("window");
ImGui::SliderFloat("##slider", &value, 0.f, 1.f);
const ImVec2 min = ImGui::GetItemRectMin(), max = ImGui::GetItemRectMax();
slider_center = ImVec2((min.x + max.x) / 2, (min.y + max.y) / 2);
ImGui::End();
}
// Presses the left button at pos, or releases it, and draws the slider in two frames.
void mouse(const ImVec2& pos, bool down)
{
ImGui::GetIO().MousePos = pos;
ImGui::GetIO().MouseDown[0] = down;
frame([this] { draw_slider(); });
frame([this] { draw_slider(); });
}
char text[16] = "";
float value = 0.f;
ImVec2 slider_center;
};
void draw_modal()
@@ -84,6 +104,28 @@ void draw_modal()
ImGui::EndPopup();
}
void draw_dropdown(bool open)
{
ImGui::Begin("window");
if (open)
ImGui::OpenPopup("dropdown");
if (ImGui::BeginPopup("dropdown")) {
ImGui::Text("item");
ImGui::EndPopup();
}
ImGui::End();
}
wxKeyEvent key_event(wxEventType type, int key_code, int modifiers = wxMOD_NONE)
{
wxKeyEvent evt(type);
evt.m_keyCode = key_code;
evt.SetControlDown(modifiers & wxMOD_CONTROL);
evt.SetShiftDown(modifiers & wxMOD_SHIFT);
evt.SetAltDown(modifiers & wxMOD_ALT);
return evt;
}
} // namespace
TEST_CASE("a vertical wheel event steps ImGui's wheel in its own direction", "[ImGuiWrapper]")
@@ -103,6 +145,21 @@ TEST_CASE("a wheel event with no rotation or on the horizontal axis does not ste
CHECK_THAT(wheel_after(rotation, axis), WithinAbs(0.0, 0.0));
}
TEST_CASE("a mouse event replaces a modifier whose key-up went to another window", "[ImGuiWrapper][Regression]")
{
ImGuiWrapper imgui;
imgui.set_display_size(800.f, 600.f);
ImGui::GetIO().KeyCtrl = true;
wxMouseEvent move(wxEVT_MOTION);
imgui.update_mouse_data(move);
CHECK_FALSE(ImGui::GetIO().KeyCtrl);
move.SetControlDown(true);
imgui.update_mouse_data(move);
CHECK(ImGui::GetIO().KeyCtrl);
}
TEST_CASE("an InputText counts as active only while it holds the keyboard", "[ImGuiWrapper]")
{
ImGuiFrames f;
@@ -161,3 +218,137 @@ TEST_CASE("ImGui owns every key under a modal and text-editing keys in an active
});
CHECK(f.imgui.owns_key(save));
}
TEST_CASE("ImGui owns the whole keyboard under a modal or in an active InputText", "[ImGuiWrapper]")
{
ImGuiFrames f;
CHECK_FALSE(f.imgui.owns_keyboard());
f.frame([&] { f.draw_input(true); });
f.frame([&] { f.draw_input(false); });
CHECK(f.imgui.owns_keyboard());
f.frame([] {});
CHECK_FALSE(f.imgui.owns_keyboard());
f.frame([] {
ImGui::OpenPopup("modal");
draw_modal();
});
CHECK(f.imgui.owns_keyboard());
}
TEST_CASE("an open dropdown owns the whole keyboard until it is closed", "[ImGuiWrapper]")
{
ImGuiFrames f;
f.frame([] { draw_dropdown(true); });
CHECK(f.imgui.popup_open());
CHECK(f.imgui.owns_keyboard());
f.imgui.close_top_popup();
f.frame([] { draw_dropdown(false); });
CHECK_FALSE(f.imgui.popup_open());
CHECK_FALSE(f.imgui.owns_keyboard());
}
TEST_CASE("closing the top popup leaves a modal open", "[ImGuiWrapper]")
{
ImGuiFrames f;
f.frame([] {
ImGui::OpenPopup("modal");
draw_modal();
});
f.imgui.close_top_popup();
f.frame([] { draw_modal(); });
CHECK(f.imgui.modal_popup_open());
}
TEST_CASE("a held slider owns the keyboard but not the keys Global shortcuts use", "[ImGuiWrapper]")
{
ImGuiFrames f;
f.mouse(ImVec2(0, 0), false);
CHECK_FALSE(f.imgui.item_held());
f.mouse(f.slider_center, true);
CHECK(f.imgui.item_held());
CHECK(f.imgui.owns_keyboard());
CHECK_FALSE(f.imgui.owns_key(KeyChord{ 'M' }));
CHECK_FALSE(f.imgui.owns_key(KeyChord{ WXK_LEFT }));
f.mouse(f.slider_center, false);
CHECK_FALSE(f.imgui.owns_keyboard());
}
TEST_CASE("a slider typed into once is not a text field when dragged", "[ImGuiWrapper][Regression]")
{
ImGuiFrames f;
f.mouse(ImVec2(0, 0), false);
ImGui::GetIO().KeyCtrl = true; // Ctrl+click types a value
f.mouse(f.slider_center, true);
ImGui::GetIO().KeyCtrl = false;
f.mouse(f.slider_center, false);
REQUIRE(f.imgui.text_input_active());
f.mouse(ImVec2(0, 0), true); // a click elsewhere closes the field
f.mouse(ImVec2(0, 0), false);
f.mouse(f.slider_center, true);
REQUIRE(ImGui::GetInputTextState(ImGui::GetActiveID()) != nullptr);
CHECK_FALSE(f.imgui.text_input_active());
CHECK_FALSE(f.imgui.owns_key(KeyChord{ 'M' }));
}
TEST_CASE("a key's char event gets its key-down's answer", "[ImGuiWrapper]")
{
ImGuiFrames f;
f.frame([&] { f.draw_input(true); });
f.frame([&] { f.draw_input(false); });
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'A')));
f.frame([] {}); // the field closes before the char arrives
REQUIRE_FALSE(f.imgui.text_input_active());
CHECK(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'a')));
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'B')));
f.frame([&] { f.draw_input(true); }); // the field opens before the char arrives
f.frame([&] { f.draw_input(false); });
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'b')));
// A char with no key-down before it is decided on its own.
CHECK(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'c')));
}
TEST_CASE("a held key's repeats and release get its press's answer", "[ImGuiWrapper]")
{
ImGuiFrames f;
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT)));
f.frame([&] { f.draw_input(true); });
f.frame([&] { f.draw_input(false); });
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT)));
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, WXK_RIGHT)));
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT)));
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, WXK_RIGHT)));
// The release also clears the press's answer for a later char with no key-down.
f.frame([] {});
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'a')));
}
TEST_CASE("a key pressed again after the canvas lost the focus is decided afresh", "[ImGuiWrapper]")
{
ImGuiFrames f;
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'M')));
f.imgui.forget_key_claims(); // the key-up went to the window that took the focus
f.frame([&] { f.draw_input(true); });
f.frame([&] { f.draw_input(false); });
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'M')));
}
TEST_CASE("an active ImGui field claims every key, whether it edits text or not", "[ImGuiWrapper]")
{
const int key = GENERATE(int('A'), int('D'), int(WXK_SHIFT));
ImGuiFrames f;
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, key, wxMOD_CONTROL)));
CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, key, wxMOD_CONTROL)));
f.frame([&] { f.draw_input(true); });
f.frame([&] { f.draw_input(false); });
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, key, wxMOD_CONTROL)));
CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, key, wxMOD_CONTROL)));
}