imgui
imgui copied to clipboard
ImGui::SetKeyboardFocusHere doesn't highlight selected item
Version/Branch of Dear ImGui:
Version 1.90.0, Branch: docking
Back-ends:
imgui_impl_glfw.cpp + imgui_impl_opengl2.cpp
Compiler, OS:
Windows 10
Full config/build information:
No response
Details:
Hello, I am trying to make a programmable change of the focus of the keyboard on the item using SetKeyboardFocusHere();; I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed. The object in this case does not lose the highlight when the mouse is moved away.
Example code:
ImGui::Selectable(std::format("Name: \"{0}\"", (t->m_name.c_str()) ? t->m_name.c_str() : "").c_str());
if (ImGui::IsItemClicked())
{
SetKeyboardFocusHere();
}
ImGui::Selectable(std::format("Mask Name: \"{0}\"", (t->m_mask_name.c_str()) ? t->m_mask_name.c_str() : "").c_str());
if (ImGui::IsItemClicked())
{
SetKeyboardFocusHere();
}
ImGui::Selectable(std::format("Width: {0}", t->m_width).c_str());
if (ImGui::IsItemClicked())
{
SetKeyboardFocusHere();
}
Screenshots/Video:
Got:
Expected:
Minimal, Complete and Verifiable Example code:
No response
I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed.
The nav highlight is intentionally hidden when the mouse is used since the highlight is only intended only for keyboard and gamepad navigation, so you're fighting against the internal design of the nav system.
It would be helpful to know exactly what you're trying to accomplish UI-wise from a user perspective as this feels very XY problem.
Are you wanting the nav highlight to always be visible when the user interacts with all items or just for these selectables?
If the latter, why? And could the built-in selectable highlighting suitable for your use-case?
As an aside, ImGui::SetKeyboardFocusHere();
without any arguments focuses the next item, you need to use ImGui::SetKeyboardFocusHere(-1);
I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed.
The nav highlight is intentionally hidden when the mouse is used since the highlight is only intended only for keyboard and gamepad navigation, so you're fighting against the internal design of the nav system.
It would be helpful to know exactly what you're trying to accomplish UI-wise from a user perspective as this feels very XY problem.
Are you wanting the nav highlight to always be visible when the user interacts with all items or just for these selectables?
If the latter, why? And could the built-in selectable highlighting suitable for your use-case?
As an aside,
ImGui::SetKeyboardFocusHere();
without any arguments focuses the next item, you need to useImGui::SetKeyboardFocusHere(-1);
Hello,
The goal I want to achieve is when I press the mouse left click button on an item or use the UP/DOWN keys, it is selected in TreeNodeEX. When I move through the navigation, the item is selected there all the time, so I thought that when I change the focus of the keyboard, the selection automatically appears.
My code at the moment works like this: Every time I press the Up/Down key, I change the state of the variable: m_keyboard_emulate_mouse_click to true, and when the button is released, to false.
int main(int argc, char* argv[]) { ... glfwSetKeyCallback(g_Window, Application::KeyHandlerCallback); ... }
void Application::KeyHandlerCallback(GLFWwindow* window, s32 key, s32 scancode, s32 action, s32 mods) { ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);
if (action == GLFW_PRESS)
{
...
if ((key == GLFW_KEY_UP || key == GLFW_KEY_DOWN))
{
g_App->m_keyboard_emulate_mouse_click = true;
}
...
}
else if (action == GLFW_RELEASE)
{
if ((key == GLFW_KEY_UP || key == GLFW_KEY_DOWN))
{
g_App->m_keyboard_emulate_mouse_click = false;
}
}
}
For each item in the tree, I check:
bool Application::IsItemClicked(ImGuiMouseButton mouse_button) { return (ImGui::IsMouseClicked(mouse_button) || m_keyboard_emulate_mouse_click) && ImGui::IsItemHovered(ImGuiHoveredFlags_None); }
whether a button was pressed, or an up or down button was pressed, and whether we hovered the mouse over an item. I use this function for each check if we have clicked something in the tree.
The operation of the program looks like this using this code:
https://youtu.be/cX2P973gR_0
I can make it work if I set the ImGuiNavHighlightFlags_AlwaysDraw flag that seems to be unused in the ImGui::RenderNavHighlight function. Changing RenderFrame, so it triggers g.NavId == id as well would also be nice to implement. I think it would be nice to set some flag that forces widgets to be rendered this way. In other guis it seems to be a thing. This way highlights for the currently focused item will be persistent between the mouse clicking and nagivation movement.
Virtual TreeView Example: https://youtu.be/TgXnG67DGko Example with the mentioned changes in ImGui code: https://youtu.be/WTFlaJYAktg