Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSX: KeyCtrl doesn't release when released in another app (e.g. when using Cmd+Tab) #3832

Closed
jparismorgan opened this issue Feb 22, 2021 · 6 comments

Comments

@jparismorgan
Copy link

jparismorgan commented Feb 22, 2021

Full info

Dear ImGui 1.80 WIP (17911)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __APPLE__
define: __GNUC__=4
define: __clang_version__=12.0.0 (clang-1200.0.32.28)
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_osx
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000040
 DockingEnable
io.ConfigViewportsNoDecoration
io.ConfigMacOSXBehaviors
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMoveFromTitleBarOnly
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000100A
 HasMouseCursors
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------

Version/Branch of Dear ImGui:

Version: 1.80 WIP - commit 388ca563dbb0fa683d2a8fbf1daaf2ab77f98a21
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.h + imgui_impl_osx.h
Compiler: clang-1200.0.32.28
Operating System: OS X

My Issue/Question:

When on a mac, you can use "Cmd + Tab" to quickly jump to another window. When you do that and then come back, ImGui::GetIO().KeyCtrl will be true even if you're not pressing down the Cmd key.

Standalone, minimal, complete and verifiable example: (see #2261)
Run the following code. Then use "Cmd + Tab" to go to another window, then come back. You'll see that KeyShift: true.

void cmdRepro() {
    ImGuiIO &io = ImGui::GetIO();

    bool IsWindowAppearing = ImGui::IsWindowAppearing();
    bool IsWindowHovered = ImGui::IsWindowHovered();
    bool IsItemHovered = ImGui::IsItemHovered();
    bool RightDown = ImGui::IsMouseDown(ImGuiMouseButton_Right);
    bool KeySuper = io.KeySuper;
    bool KeyCtrl = io.KeyCtrl;
    bool KeyAlt = io.KeyAlt;
    bool KeyShift = io.KeyShift;

    ImGui::Text("IsWindowAppearing: %d IsWindowHovered: %d IsItemHovered: %d RightDown: %d KeySuper: %d KeyCtrl: %d KeyAlt: %d KeyShift: %d",
      IsWindowAppearing,
      IsWindowHovered, IsItemHovered, RightDown, KeySuper,
      KeyCtrl,
      KeyAlt,
      KeyShift
    );
}

Workaround:
You can manually reset it with:

    // If you cmd + tab to another window then come back io.KeySuper will be true. Reset it.
    if (ImGui::IsWindowAppearing()) {
      ImGui::GetIO().KeySuper = false;
    }
@ocornut ocornut changed the title ImGui::GetIO().KeyCtrl is true even when not pressing down key on Mac when you use "Cmd + Tab" to go to another window and then come back - imgui_impl_opengl3.h + imgui_impl_osx.h OSX: KeyCtrl doesn't release when released in another app (e.g. when using Cmd+Tab) Feb 24, 2021
@ocornut
Copy link
Owner

ocornut commented Feb 24, 2021

you can use "Cmd + Tab" to quickly jump to another window.

I don't know what Cmd+Tab is mapped to. It is switching to other OS window (OSX ui) or other Dear ImGui window? (dear imgui "ctrl+tab" window). Given your ConfigFlags I am assuming keyboard nav is disabled so it is the earlier, but it is bizarre you suggest using IsWindowAppearing() which is testing for Dear ImGui windows appearing whereas the suggestion relate to OS window gaining/losing focus.

I believe this is the same as #3532 but specifically for the OSX backend.

@jparismorgan
Copy link
Author

jparismorgan commented Feb 24, 2021

you can use "Cmd + Tab" to quickly jump to another window.

I don't know what Cmd+Tab is mapped to. It is switching to other OS window (OSX ui) or other Dear ImGui window? (dear imgui "ctrl+tab" window). Given your ConfigFlags I am assuming keyboard nav is disabled so it is the earlier, but it is bizarre you suggest using IsWindowAppearing() which is testing for Dear ImGui windows appearing whereas the suggestion relate to OS window gaining/losing focus.

@ocornut Good question, should have been more specific. It's switching to a different OS window (through the OSX ui). I also tested more and you're correct, using IsWindowAppearing() is not a proper fix for this, it still happens.

I believe this is the same as #3532 but specifically for the OSX backend.

Yes, looks like the same thing, but with the OSX backend and with the command key.

In the other thread you suggest "arbitrary clearly all inputs while unfocused is a typical workaround people use on this sort of case, but is not a generally correct/valid solution" - are you basically suggesting to set ImGui::GetIO().KeySuper to false when IsWindowFocused() is false? Or is there a different API you were suggesting?

@ocornut
Copy link
Owner

ocornut commented Feb 24, 2021

I was referring to back-end clearing all Keys[] and key modifiers when all dear imgui windows are unfocused.
This is probably the better default solution to implement across all back-ends, which maybe an option to disable it.

@rokups
Copy link
Contributor

rokups commented Feb 25, 2021

What if you alt-tab to another window, keep holding alt and then alt-tab back? No new "key-down" event would fire, alt would remain held down but down state would be cleared as window regained focus.

@ocornut
Copy link
Owner

ocornut commented Mar 23, 2021

What if you alt-tab to another window, keep holding alt and then alt-tab back? No new "key-down" event would fire, alt would remain held down but down state would be cleared as window regained focus.

That's technically a bug but pragmatically this is less problematic and a MUCH better default than currently and many apps behave this way. I'll now be applying this clearing in the Win32 backend and would appreciate the equivalent PR for native OSX backend.

rokups added a commit to rokups/imgui that referenced this issue Apr 19, 2021
ocornut pushed a commit that referenced this issue Apr 19, 2021
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2021

Merged fix by @rokups: 6d53884

@ocornut ocornut closed this as completed Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants