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

SetKeyboardFocusHere(-1) not working with InputTextMultiline #4761

Closed
actondev opened this issue Nov 24, 2021 · 7 comments
Closed

SetKeyboardFocusHere(-1) not working with InputTextMultiline #4761

actondev opened this issue Nov 24, 2021 · 7 comments

Comments

@actondev
Copy link

Version: v1.85
Branch: master

When doing something like

InputTextMultiline(..);
// checking for right click
if(IsItemHovered() && IsItemClicked(1)) {
SetKeyboardFocusHere(-1);
}

Nothing happens.
However, when using InputText instead of InputTextMultiline, it works as expected!

Another note: putting the SetKeyboardFocusHere(0) before the InputTextMultilinedoes have effect (not useful but nonetheless).

Some context: I noticed that only with left mouse-click does an inputText gets focus & wanted to have it also happening with the right click. But as I saw in the implementation, the MouseClicked[0] etc checks are hardcoded, taking into account only the left click.

@ocornut
Copy link
Owner

ocornut commented Nov 29, 2021

I have pushed a fix for this 66f0fb9 and added regression tests.
It's a little complicated because of how InputTextMultiline() uses a child window + a scrollbar and manipulating the scrollbar should report the item as active, leading to many subtle complications. I believe we could consider reworking the widget to not use a child window at all... it'll fix some issues.

@ocornut ocornut closed this as completed Nov 29, 2021
@ocornut ocornut reopened this Nov 30, 2021
@ocornut
Copy link
Owner

ocornut commented Nov 30, 2021

Reopening as this commit broke tabbing through InputTextMultiline() multiline.

ocornut added a commit that referenced this issue Nov 30, 2021
…3092)

Messy... Broken by 66f0fb9. Added ImGuiItemFlags_NoTabStop to EndGroup() ahead of time (not strictly needed here).
@ocornut
Copy link
Owner

ocornut commented Nov 30, 2021

Pushed a fix c5db276.
It's not great, will simplify and tidy up the mess as we attempt to remove the child window from InputTextMultiline()..

@ocornut ocornut closed this as completed Nov 30, 2021
@SimonMaracine
Copy link

SimonMaracine commented Aug 7, 2024

Hello! I'm not sure if I should open a new issue, because it seems that SetKeyboardFocusHere(-1) works weirdly with InputTextMultiline in version 1.91.1 WIP, docking branch.
I want to reclaim focus on the InputTextMultiline whenever I press Enter. I followed the console example from imgui_demo.cpp. But it doesn't work with InputTextMultiline. I press Enter, but it still looses focus:

bool reclaim_focus {false};

if (ImGui::InputTextMultiline("##", ..., ..., ..., ImGuiInputTextFlags_CtrlEnterForNewLine | ImGuiInputTextFlags_EnterReturnsTrue)) {
    // ...
    reclaim_focus = true;
}

if (reclaim_focus) {
    ImGui::SetKeyboardFocusHere(-1);
    std::cout << "pressed enter\n";
}

However, this works, but it makes it impossible to press other buttons:

if (ImGui::InputTextMultiline("##", ..., ..., ..., ImGuiInputTextFlags_CtrlEnterForNewLine | ImGuiInputTextFlags_EnterReturnsTrue)) {
    // ...
}

ImGui::SetKeyboardFocusHere(-1);

I guess it works in the second example, because it sets the focus every frame, not only in the frame that I pressed Enter. But I expect the former to work, because it already works with InputText. I'm sorry if I actually did something wrong or I misunderstood something.

@ocornut ocornut reopened this Aug 7, 2024
@Skulaurun
Copy link

Same issue on dear imgui, v1.90.4 master branch. I have followed the tutorial for Debug Console in imgui_demo.cpp, and after I changed InputText to InputTextMultiline, the ImGui::SetKeyboardFocusHere(-1) stopped restoring focus after pressing Enter.

ImGuiInputTextFlags inputFlags = ImGuiInputTextFlags_EnterReturnsTrue
    | ImGuiInputTextFlags_EscapeClearsAll
    | ImGuiInputTextFlags_AllowTabInput
    | ImGuiInputTextFlags_CtrlEnterForNewLine;
if (ImGui::InputTextMultiline("Lingo v0.1", inputBuffer.data(), inputBuffer.size(), {}, inputFlags)) {
    std::size_t dataLength = strlen(inputBuffer.data());
    execute(inputBuffer.substr(0, dataLength));
    std::memset(inputBuffer.data(), '\0', dataLength);
    ImGui::SetKeyboardFocusHere(-1);
}

Is there a workaround to keep the multiline text input focused after pressing enter?

@ocornut
Copy link
Owner

ocornut commented Aug 7, 2024

Please open a new issue for increased visibility.

@ocornut
Copy link
Owner

ocornut commented Aug 21, 2024

Fixed again 521f84a + extra test ocornut/imgui_test_engine@633988e

@ocornut ocornut closed this as completed Aug 21, 2024
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

4 participants