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

What is a correct way to handle keyboard input for active/current/selected/focused window only? #5979

Closed
MichaelKv opened this issue Dec 10, 2022 · 6 comments

Comments

@MichaelKv
Copy link

Hello,
From imgui.h:
#define IMGUI_VERSION "1.89 WIP"
#define IMGUI_VERSION_NUM 18814
#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx))
#define IMGUI_HAS_TABLE
#define IMGUI_HAS_VIEWPORT // Viewport WIP branch
#define IMGUI_HAS_DOCK // Docking WIP branch
Dx12

What is a correct way to handle keyboard input for active/current/selected/focused window only? I.e. the window in the docking environment that is highlighted at this very moment. I have two windows that handle arrow keys differently. I tried:
if (ImGui::IsItemFocused()) { if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow))) { ...

And
if (ImGui::IsItemActive ()) { if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow))) { ...

It did not work. IsItemActive worked if I held the key and clicked the window caption. So, it was bad idea indeed.
I tried the code above within Begin/End. Sorry, I was unable to find a proper example.
Thank you.

@ocornut
Copy link
Owner

ocornut commented Dec 10, 2022

Try using IsWindowFocused().

PS: GetKeyIndex() is a no-op since 1.87
You can call IsKeyPressed(ImGuiKey_UpArrow).

@MichaelKv
Copy link
Author

Thanks. It did the job. I cannot believe I have overlooked it. I see that I do not need to put my keyboard handling code after all children to use ImGuiFocusedFlags_ChildWindows flag. I.e. I have
Begin("window"...
processInputWithImGuiFocusedFlags_ChildWindows()
BeginChild("child1"...
and it works. Is it by chance and I need to put processInputWithImGuiFocusedFlags_ChildWindows() after all the children or it is right approach?
And thanks for the hint on GetKeyIndex.

@MichaelKv
Copy link
Author

I have another question. I have the following situation:
``
void processAAAInput() {
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) {
if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow)) {
void draw () {
with_Window("AAA"…
processAAAInput(ImGuiFocusedFlags_ChildWindows); // 111

with_Child("BBB"…
}
if (ImGui::Button(“CCC”…
// 222
}

processAAAInput does not handle input (arrow keys) unless I click inside the AAA window (I click the BBB child). Then it works until I press the CCC button. To make it to work again I need to click inside AAA again.
This happens regardless of processAAAInput position – 111 or 222.
I expect processAAAInput shall handle the input after I clicked the CCC button but it does not.

P.S. Sorry, the source code tag in this message does not work for me -- it draws everything in one line here.

@ocornut
Copy link
Owner

ocornut commented Dec 11, 2022

Your source code is hard to decipher. Please make an effort to create an actual minimal pastable repro, and use eg ImGui::Text() elements to visuable the state of eg IsWindowFocused() return value.

@MichaelKv
Copy link
Author

Please, never mind my last question. I never knew the focus is on the last Begin(…) window as far as I had common key handling (without a IsWindowFocused call). So, when I added IsWindowFocused my “main” window the one I drawn first did not handled any input until I clicked it. And I have left some key handlers in a child window so when I pressed the button outside it stopped working too until clicking inside.
I am really sorry.

@ocornut
Copy link
Owner

ocornut commented Dec 12, 2022

Great to hear. Closing as answered.

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

2 participants