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

How to set the current keyboard focus. #1712

Open
eriksintorn opened this issue Mar 30, 2018 · 6 comments
Open

How to set the current keyboard focus. #1712

eriksintorn opened this issue Mar 30, 2018 · 6 comments
Labels
focus nav keyboard/gamepad navigation

Comments

@eriksintorn
Copy link

Hello,
Apologies if there is an obvious answer to this, but I cannot find it. In my game, I have an inventory represented by a number of buttons. When I press a specific key, focus is shifted to that window and I can use the keyboard to navigate to the button (item) I want to use, and it is nicely highlighted.

I am wondering if there is any way to set the current keyboard focus (the currently selected item/button) in code? I've obviously tried SetKeyboardFocusHere(), but that seems to only work on InputText and similar widgets, not buttons? Or am I doing something wrong?

Any help greatly appreciated, thanks.

@eriksintorn
Copy link
Author

Okay, setting the context's .NavId seems to work for me, but is there a less brutal way to move the keyboard focus?

@ocornut ocornut added the nav keyboard/gamepad navigation label Mar 31, 2018
@ocornut
Copy link
Owner

ocornut commented Mar 31, 2018

Hello @eriksintorn,

There's no public API at the moment but you can use SetFocusID() in imgui_internal.h which essentially sets NavId. (Note that there's currently a left-over inconsistency/overlap between the words Nav and Focus that I ought to clarify)

There's a private branch I started working on with functions to Focus and Activate items, and handle Tabbing more consistently, but there was a couple of tricky unsolved issues so that's unfinished at the moment.

@eriksintorn
Copy link
Author

Perfect! To also get the selected item highlighted I'm still hacking a bit. But this will work fine until you've finished a proper API. I am amazed with the quality of this library, and the speed with which you answer questions! Thank you very much.

ImGui::SetFocusID(ImGui::GetID((name).c_str()), ImGui::GetCurrentWindow()); 
ImGuiContext& g = *ImGui::GetCurrentContext();
g.NavDisableHighlight = false;

@ocornut
Copy link
Owner

ocornut commented Mar 31, 2018

I spent 3-4 days on the proper API (which encompassed some other things such as the tabbing) and the code was becoming a little too complex so I'll need to rework it, I don't know when but hopefully within a few months.

Note that you should not want/need to ever modify imgui.cpp, you can include imgui_internal.h from your own .cpp file and add to the ImGui:: namespace from there. That will ease merging.

A word of warning though:

In my game, I have an inventory represented by a number of buttons.

Dear ImGui is really not designed for in-game UI. I believe if you go this way you will encounter a lot of small and tricky hurdles to overcome if you are aiming for high-quality in-game UI. I would personally advise against it but I know some people have been doing it.

The most accomplished case I can think of doing it is the game Deformers:
https://www.google.com/search?q=deformers+game&client=firefox-b&source=lnms&tbm=isch&sa=X&ved=0ahUKEwibi46CopbaAhWCZlAKHQFiBzkQ_AUICigB&biw=1472&bih=698

xkb2n6j

Which went the way of redrawing their own controls over using imgui underlying logic (ignoring imgui render output). Somehow apparently the directional navigation worked for them.

Personally I would suggest creating custom widgets for in-game, and you may use ImDrawList and some of the lower-level ImGui stuff if you are comfortable with that.

@eriksintorn
Copy link
Author

Dear ImGui is really not designed for in-game UI. I believe if you go this way you will encounter a lot of small and tricky hurdles to overcome if you are aiming for high-quality in-game UI. I would personally advise against it but I know some people have been doing it

No worries, we are aiming for a shockingly rudimental UI at the moment :). Thanks again.

@ocornut ocornut added the focus label Oct 10, 2018
ocornut added a commit that referenced this issue Oct 18, 2024
ocornut added a commit that referenced this issue Oct 18, 2024
…(), ImGuiNavHighlightFlags to ImGuiNavRenderCursorFlags. (#1074, #2048, #7237, #8059, #1712, #7370, #787)

+ referenced in #8057, #3882, #3411, #2155, #3351, #4722, #1658, #4050.
@ocornut
Copy link
Owner

ocornut commented Oct 18, 2024

@eriksintorn You are probably not into this anymore, but replying so there are references for others:

Perfect! To also get the selected item highlighted I'm still hacking a bit. But this will work fine until you've finished a proper API. I am amazed with the quality of this library, and the speed with which you answer questions! Thank you very much.
ImGui::SetFocusID(ImGui::GetID((name).c_str()), ImGui::GetCurrentWindow());
ImGuiContext& g = *ImGui::GetCurrentContext();
g.NavDisableHighlight = false;

(1) g.NavDisableHighlight = false can now be replaced with ImGui::SetNavCursorVisible(true);
(2) We are still lacking decent public API for focusing given an ID. So SetFocusID() is still the thing to use.

The reason for (2) is that attempting to implement this properly requires a large amount of design for features and flags, related to window focusing, opt-in/opt-out override of modals covering the target, scrolling in target window and potentially its parents, handling of highlight state and nav cursor visibility, clipping issues etc. I have been slowly converging toward assembling and designing many of the features but for this specific thing it hasn't materialized into a nice public API yet, but I feel I am getting pretty close.

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