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

Deselect window from Keyboard #3200

Closed
u3shit opened this issue May 5, 2020 · 4 comments
Closed

Deselect window from Keyboard #3200

u3shit opened this issue May 5, 2020 · 4 comments
Labels
focus nav keyboard/gamepad navigation

Comments

@u3shit
Copy link
Contributor

u3shit commented May 5, 2020

Version/Branch of Dear ImGui:

Version: 1.77 WIP
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp
Compiler: clang
Operating System: linux

My Issue/Question:

Sorry if this is a stupid question, but I can't seem to find a way to deselect a window with keyboard nav. When no window is selected, imgui will return WantCaptureKeyboard=0, but even in this case, ctrl-tab works and I can select a window. Doing this will capture the keyboard. The problem is that there doesn't seem to be a way to get back to the original all windows deselected state, because ctrl-tab only cycles between windows (and the main menu bar if there's one). With Escape I can close popup menus, but it does nothing when I have a window selected. (If there's only a main menu bar and no windows, pressing escape does actually deselect the menu bar and releases the keyboard, but as soon as there's an active window, it will just select it). Pressing tab, space, alt, arrow keys doesn't seem to help either, I can close the window by pressing alt then selecting the X button, if it's closable though. The only way I found to deselect all windows is to click with the mouse somewhere on the background.
Am I overlooking something? If not, could you add some way to deselect all windows without having to use the mouse?

Standalone, minimal, complete and verifiable example:
Start the demo, enable NavEnableKeyboard under configuration then watch WantCaptureKeyboard under Inputs, Navigation & focus

@ocornut
Copy link
Owner

ocornut commented May 8, 2020

Something would need to be added in NavUpdate() in the end of that large block:

// Process NavCancel input (to close a popup, get back to parent, clear focus)
if (IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed))

Something that would eventually call FocusWindow(NULL), e.g. at the very end of that section, replace:

g.NavId = g.NavFocusScopeId = 0;

With

if (g.NavId)
    g.NavId = g.NavFocusScopeId = 0;
else if (g.NavWindow)
    FocusWindow(NULL);

The problem being to design that exact desirable behavior and various application may need different things. For instance, the "clearing g.NavId" behavior is ALREADY something that some application are considering to be undesirable and want removed. An app that doesn't run "over" a game has no reason to clear widget focus at all, let alone window focus.

You can side-step that decision and handle the code in your own application:

#include "imgui_internal.h"
ImGuiContext& g = *GImGui;
if (ImGui::IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed))
    if (g.NavWindow && g.ActiveId == 0 && g.OpenPopupStack.empty() && g.NavLayer == ImGuiNavLayer_Main)
        ImGui::FocusWindow(NULL);

Or we design a more generic solution, and I'm guessing that maybe a single flag would be enough: either enable clearing of NavWindow/NavId all-together on ESC, either now. I'll have to think about this a little more.

Note for later:
A minor side-issue is that block is not playing along well with how we want to dispatch inputs, and I intend to move all those individual if {} blocks into the end scope of what they react to (so e.g. the "Exit child window" behavior should be polled in EndChild() etc) and it will be a bit of thoughtful testing to get that transition done right.

@u3shit
Copy link
Contributor Author

u3shit commented May 11, 2020

I've pasted the last code snipped just after ImGui::NewFrame(), and it correctly deselects the window on Escape. However it also deselects it if I press escape inside a menu.
I'm not keen on using Escape to deselect the window if it causes problems for other applications, I just like to have ANY way to deselect the window, even something like adding a new entry to the Ctrl-Tab list, but probably adding a flag is the best.

@ocornut
Copy link
Owner

ocornut commented Oct 14, 2024

Hello @u3shit, I know it's been a while and you may not have this problem anymore. However I've been interested in this topic among some others. I would be curious if you can from memory write a little bit of details on the context?

How did it matter specifically that windows were unfocused, vs just relying on WantCaptureKeyboard=0 for routing inputs to your "background" application? What's the typical situation where you expected to be able to unfocus windows with the keyboard?

I'm asking because I'll probably come up with a mix of options, one to manipulate how the Escape key affects navigation, and one potentially to add an option in the Ctrl+Tab menu to explicitly unfocus all windows.

I also don't understand this comment:

"However it also deselects it if I press escape inside a menu."

Since the code I provided intentionally tested for g.OpenPopupStack.empty() and therefore wouldn't trigger while inside a menu.

ocornut added a commit that referenced this issue Oct 14, 2024
… on Escape. (#3200)

+ pressing escape to hide nav highlight doesn't clear location from when ctrl+tabbing back into same window later.
ocornut added a commit that referenced this issue Oct 14, 2024
ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Oct 14, 2024
@ocornut
Copy link
Owner

ocornut commented Oct 14, 2024

With b001038 i have added a io.ConfigNavEscapeClearFocusWindow option to enable this feature.
I've got other related features planned but this is the only one I'm confident about the design now.

I've also fixed two other bug: pressing Escape doesn't lose/forget last location of a window + 626d358 pressing ctrl+tabbing from a state where no window is focused starts from the top most one.

@ocornut ocornut closed this as completed Oct 14, 2024
ocornut added a commit that referenced this issue Oct 18, 2024
…ways. (#1074, #2048, #7237, #8059, #3200, #787)

Note: the NavCursorHideFrames addition is to support 88a3545 even though ConfigNavCursorVisibleAlways is set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus nav keyboard/gamepad navigation
Projects
None yet
Development

No branches or pull requests

2 participants