Skip to content

How to detect enter key being pressed on InputDouble? #9117

Description

@fghoussen

Version/Branch of Dear ImGui:

a959617 unmodified docking

Back-ends:

imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp

Compiler, OS:

g++-15.2, debian/linux

Full config/build information:

No response

Details:

My Issue/Question:

How to detect enter key being pressed on InputDouble?

The problem is to update a vector with normalized xyz values only after the user has finished to type the value of one component of the vector. I guess the problem here is that xyz are "re-entrant" as they are both modified by the GUI and the (normalized) vector: when you type a value for xyz and you are not done typing yet, the GUI displays some kind of crazy continuously changing values for xyz! (picture attached). After pressing enter key, values are stable.

I tried:

  • Different flavors of if statements using xyzModif and ImGui::IsItemDeactivatedAfterEdit (see code attached): with this, the GUI has either "crazy" behavior or isn't updated.
  • ImGuiInputTextFlags_EnterReturnsTrue: seems supported only for text (not for scalar)

Is there a way to handle or workaround this case?

Screenshots/Video:

Image Image

Minimal, Complete and Verifiable Example code:

$ git diff

+double x = 1.;
+double y = 2.;
+double z = 3.;
+bool normalize = true;
+
 void initializeFrame() {
   // Clear.
   glClear(GL_COLOR_BUFFER_BIT);
@@ -95,7 +101,21 @@ void initializeFrame() {
 
   // Create main thread GUI.
   ImGui::Begin("Main thread");
+  bool xyzModif = false;
+  xyzModif |= ImGui::InputDouble("X", &x);
+  xyzModif |= ImGui::InputDouble("Y", &y);
+  xyzModif |= ImGui::InputDouble("Z", &z);
+  ImGui::Checkbox("Normalize", &normalize);
+  if (normalize) {
+  //if (xyzModif && normalize) {
+  //if (ImGui::IsItemDeactivatedAfterEdit() && normalize) {
+    double norm = std::sqrt(x*x + y*y + z*z);
+    if (norm > 1.e-6) {
+      x /= norm;
+      y /= norm;
+      z /= norm;
+    }
+  }
   ImGui::End();
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions