imgui: Assertion failed: g.IO.KeyMods with GLFW backend

Dear ImGui 1.77 (17700)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 4, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1926
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000000
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMemoryCompactTimer = 60.0f
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.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

I’m running into something that I can’t seem to get to the bottom of, and I’m hoping you can help. I’m using a framework on top of ImGui, which implements key bindings and actions. I’m running into a situation where when I execute an action from a key binding, and then I launch a native file dialog, I’m running into an assert in ImGui when the dialog returns control back to ImGui.

I get the following assert:

Assertion failed: g.IO.KeyMods == expected_key_mod_flags && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods", file ..\imgui\imgui.cpp, line 6765

My code is structured like this:

inline void host::draw(const ImVec2& size) {
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    _app->draw(ImGui::GetIO().DisplaySize);
    _app->respond();

    ImGui::Render();
    glViewport(0, 0, size.x, size.y);
    glClearColor(_bg.x, _bg.y, _bg.z, _bg.w);
    glClear(GL_COLOR_BUFFER_BIT);
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    glfwSwapBuffers(_window);
}

void workspace_app::respond() {
    auto io = ImGui::GetIO();
    if(!io.WantCaptureKeyboard) {
        for(const auto& action : _actions) {
            if(action->triggered(io)) {
                action->execute();
                return;
            }
        }
    }
}

bool action::triggered(const ImGuiIO& io) const {
    return _key_binding->triggered(io);
}

bool key_binding::triggered(const ImGuiIO& io) const {
    return (io.KeyCtrl == ctrl && io.KeyAlt == alt && io.KeyShift == shift && ImGui::IsKeyPressed(key));
}

The workspace_app::respond is the concrete overload responding to the _app->respond() line in host::draw above.

When I invoke this same code path through clicking on a user interface element, the code path works perfectly. It only triggers this assert when I use the keyboard shortcut (CTRL-S) in my case, to invoke the code path. If I change my keyboard shortcut to just S instead of CTRL-S, it also works perfectly. It only seems to hit this assert when modifier keys are involved.

Any insight would be very appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 29 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Commit with this fix: 3600ceb527bc4877691d5679f45292852932a115

Commit where the check was first added: ccf0cc85840a5edbeb7c4450cfd67949d0534512 It’s an important check as a frequent error was to call events after ImGui::NewFrame() and while things would appear to mostly work, some of the nav queries done in NewFrame would essentially break.

Thanks all for your patience with this, glad we got this through.

I can probably put something together this weekend.