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 detect when an item is in text input mode? #3556

Closed
lfnoise opened this issue Oct 23, 2020 · 5 comments
Closed

How to detect when an item is in text input mode? #3556

lfnoise opened this issue Oct 23, 2020 · 5 comments

Comments

@lfnoise
Copy link
Contributor

lfnoise commented Oct 23, 2020

Version: 1.80 WIP (17905)
Branch: master
Back-ends: imgui_impl_metal.mm + imgui_impl_osx.mm
Compiler: clang/Xcode
Operating System: macos 10.15.4

If I am using SliderFloat and I want to have my app ignore the value while it is being text edited, how do I do that? That is, I want to detect when the widget is in text input mode and not pass the value along to my app while it is being edited. I do want to track the value while the slider is being moved by the mouse. I had thought that IsItemFocused() was the way to do this. But IsItemFocused() does not return true when SliderFloat is in text input mode. Nor does it return true when InputText or InputFloat are in edit mode.

static float f = 0.0f;
float copy_of_f = f;
bool changed = ImGui::SliderFloat("float", &copy_of_f, 0.0f, 1.0f);
bool focused = ImGui::IsItemFocused();
bool active = ImGui::IsItemActive();
if (changed && !focused) {
    // I expected this to only happen when not in text input mode.
    f = copy_of_f;
}
printf("f changed %d focused %d active %d %f\n", changed, focused, active, f);
@ocornut
Copy link
Owner

ocornut commented Oct 26, 2020

You may use ImGui::IsItemActive() && ImGui::TempInputIsActive(ImGui::GetActiveID()) from imgui_internal.h
However I would strongly advise to consider that:

  • Maybe your underlying problem can be better solved by using ImGuiSliderFlags_AlwaysClamp (input value will never be out of bounds)
  • Maybe your underlying problem can be better solved by IsItemDeactivated() or IsItemDeactivatedAfterEdit() ?

(linking to #701 for future ref for when we implement it.)

printf("f changed %d focused %d active %d %f\n", changed, focused, active, f);

IsItemFocused() is the keyboard/gamepad navigation focus.
When you want to debug that sorts of things, consider using ImGui::Text() calls instead of printf. A stable display is easier to read than a log. Also see Demo>Widgets>Querying Status.

@lfnoise
Copy link
Contributor Author

lfnoise commented Oct 26, 2020

Thanks for the response.

ImGuiSliderFlags_AlwaysClamp is not a solution for my case. The slider is used as a parameter for a real time audio synthesis process, and jumping to a value which only represents the first two digits typed of a frequency which might be in the range of 20-20000 for example would cause an unwanted glitching of the audio.

I will investigate IsItemDeactivated and IsItemDeactivatedAfterEdit. Thanks also for the debugging tip.

@ocornut
Copy link
Owner

ocornut commented Oct 26, 2020 via email

@lfnoise
Copy link
Contributor Author

lfnoise commented Oct 26, 2020

No, I want it live while dragging the slider, but not while typing.
Full disclosure: I am writing my own slider widgets for imgui. I found I can add my own state variable and query it and so far it seems to work.
sapf_sliders_for_imgui

@ocornut
Copy link
Owner

ocornut commented Oct 26, 2020

Very nice!

@ocornut ocornut closed this as completed Oct 28, 2020
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