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 get the "Enter" key to generate an ImGui::Button press? #5606

Closed
njk42 opened this issue Aug 23, 2022 · 5 comments
Closed

How to get the "Enter" key to generate an ImGui::Button press? #5606

njk42 opened this issue Aug 23, 2022 · 5 comments
Labels
inputs nav keyboard/gamepad navigation

Comments

@njk42
Copy link

njk42 commented Aug 23, 2022

Version: 1.88
Branch: master
Back-ends: GLFW and OpenGL3
Operating System: Windows

I have searched the documentation and example code, as well as experimented with all of the Widgets in the imgui_demo.cpp, but
I cannot seem to figure out one simple workflow which seems (at least to me) to be something which would be enabled by default:

That is, using a press of the "Enter" key to invoke the action of an ImGui::Button which has "focus" in the same way that it would be invoked by a mouse click on that button.

It appears to me that the Tab, Arrows, and Space keys are very useful at being able to move the focus and navigate thru the editing state of Input widgets, expand/contract trees, and other Widget actions, but I cannot figure out a way to make the Enter key the same as a mouse click on a simple Button. (For example, I have a Modal window with a single 'OK' button and I want to be able to press the Enter key to close it instead of mousing to the OK button and clicking.)

Am I missing something obvious, or is this not supported?

Yes, I do have the following line uncommented in the example code:
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls

@PathogenDavid
Copy link
Contributor

PathogenDavid commented Aug 23, 2022

I don't believe there's any built-in way to get this behavior. Currently only the space bar triggers button presses. (Dear ImGui uses enter to enable editing (for editable widgets) and space for activation.)

That being said it doesn't seem totally unreasonable that a button should react to enter being pressed since it can't be edited and it'd be consistent with Windows, so I'd wait to see what Omar says.


If you want this sooner rather than later you could either tweak ImGui::ButtonBehavior to react to enter being pressed when it has focus or make your own helper function which wraps ImGui::Button, something like this:

static bool MyButton(const char* label, const ImVec2& size = ImVec2(0, 0))
{
    return ImGui::Button(label, size) || (ImGui::IsItemFocused() && ImGui::IsKeyPressed(ImGuiKey_Enter));
}

@ocornut ocornut added the nav keyboard/gamepad navigation label Aug 23, 2022
@ocornut
Copy link
Owner

ocornut commented Aug 23, 2022

We should probably amend existing behavior to allow this by default.

ocornut added a commit that referenced this issue Mar 7, 2023
… key. (#5606)

Instead of adding NavActivateInputId support in ButtonBehavior() started untangling the mess.
ocornut added a commit that referenced this issue Mar 7, 2023
… key. (#5606)

Instead of adding NavActivateInputId support in ButtonBehavior() started untangling the mess.
@ocornut
Copy link
Owner

ocornut commented Mar 7, 2023

I have pushed this change with c9a53aa, Enter now activates for navigation and I have reduced the difference between Space and Enter.

@ocornut ocornut closed this as completed Mar 7, 2023
kjblanchard pushed a commit to kjblanchard/imgui that referenced this issue May 5, 2023
… key. (ocornut#5606)

Instead of adding NavActivateInputId support in ButtonBehavior() started untangling the mess.
@crazydef
Copy link

Is there a way to make a button the default button in a window, so that pressing Enter will activate that button even if it doesn't have focus?

I see there's nothing in ImGuiButtonFlags, so I'm guessing this isn't currently possible?

@ocornut ocornut added the inputs label Nov 30, 2023
@ocornut
Copy link
Owner

ocornut commented Nov 30, 2023

It's currently not possible but eventually planned.
You can easily use e.g. if (ImGui::Button("OK") || (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsWindowFocused()) as an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inputs nav keyboard/gamepad navigation
Projects
None yet
Development

No branches or pull requests

4 participants