Repository navigation
Description
Version/Branch of Dear ImGui:
Version 1.91.3, Branch: docking
Back-ends:
imgui_impl_android.cpp + imgui_impl_opengl3.cpp
Compiler, OS:
ndk, Android
Full config/build information:
No response
Details:
tldr: how to unfocus InputText programmatically and still trigger the IsItemDeactivatedAfterEdit?
i have a special use-case on how i handle InputText, im using imgui on android devices and my way of handling keyboard is to popup an input dialog, send the value to c++ and then explitly set the InputText buffer inside ImGuiInputTextCallback like so (do tell me if there's a better way):
static auto InputTextCallbackAlways(ImGuiInputTextCallbackData *data) -> int
{
if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways)
{
if (g_SetInputText)
{
{
bool changed = false;
for (auto it = g_InputTextBuf.begin(); it != g_InputTextBuf.end();)
{
unsigned int c = *it;
if (!InputTextFilterCharacter(GImGui, &c, data->Flags, nullptr, nullptr, false))
{
it = g_InputTextBuf.erase(it);
changed = true;
}
else
{
++it;
}
}
if (changed)
{
s_App->handleInputTextChange(g_InputTextBuf);
}
// char *end = fmt::format_to(data->Buf, "{}", g_InputTextBuf);
// *end = '\0';
strcpy(data->Buf, g_InputTextBuf.c_str());
data->BufTextLen = (int)g_InputTextBuf.size();
data->BufDirty = true;
// data->CursorPos = data->SelectionStart = data->SelectionEnd = (int)strlen(data->Buf);
g_InputTextBuf.clear();
}
g_SetInputText = false;
}
}
// else
if (userData->ChainCallback)
{
data->UserData = userData->ChainCallbackUserData;
return userData->ChainCallback(data);
}
return 0;
}
now i managed to emulate the behaviour as if user press Enter on a real keyboard as seen below, however i fail to emulate the unfocus behaviour, for example ImGui::IsItemDeactivatedAfterEdit would not return true
static char s_Buf[256];
ImGui::InputText("Text", s_Buf, IM_ARRAYSIZE(s_Buf));
if (ImGui::IsItemDeactivatedAfterEdit())
{
LOG_INFO("Text: {}", s_Buf); // This code would not execute
}the isEnter is true if user press the Enter on their soft-keyboard, otherwise it would be false if the popup dialog is closed, i want to unfocus the InputText if the popup dialog is closed, which should trigger IsItemDeactivatedAfterEdit, but i fail to do so, any help would be appreciated
void nativeTextDialogAction(JNIEnv *env, jclass clazz, jstring text, jboolean isEnter) // NOLINT
{
if (isEnter)
{
ImGui::GetIO().AddKeyEvent(ImGuiKey_Enter, true);
ImGui::GetIO().AddKeyEvent(ImGuiKey_Enter, false);
}
else
{
ImGui::ClearActiveID();
// ImGui::SetWindowFocus(nullptr); // unfocus window to clear keyboard focus
// ImGui::SetWindowFocus(); // refocus window
}
}Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
No response
Activity
ocornut commentedon Jan 10, 2025
What are you doing this and not simply calling
io.AddInputCharacter()with the contents of g_InputTextBuf ?I believe it should then behave 100% normally.
Note that nowadays to replace the text you can do:
And it will reload from user provided buffer.
[-]how to unfocus InputText programmatically[/-][+]How to unfocus InputText programmatically[/+]catlowlevel commentedon Jan 10, 2025
i am also manuallly setting the selection (which has been omitted from the example)
i believe that's not possible with
io.AddInputCharacter()?i've seen this function but never understood how and where to use it
is it possible to set cursor/selection there?
ocornut commentedon Jan 10, 2025
Look at the other functions and how they are implemented...
catlowlevel commentedon Jan 10, 2025
yes, i've just look at it.
but how can i set the buffer when i don't have access to it?
anyway, i don't think this is relevant to what im trying to achieve
ClearActiveIDdoes not do this unfortunatelyocornut commentedon Jan 10, 2025
I can confirm this doesn't seem to work, and I will investigate it.
However I also separately think that it is unclear why you can't have your virtual keyboard send only io events and it would work. Can you clarify what you are trying to achieve with this keyboard?
[-]How to unfocus InputText programmatically[/-][+]How to unfocus InputText programmatically + IsItemDeactivatedAfterEdit() with ClearActiveID() [/+]ocornut commentedon Jan 10, 2025
I believe the
IsItemDeactivatedAfterEdit()issue is likely the same as #6766 and #5904 (and might be the same as #8004 too).catlowlevel commentedon Jan 10, 2025
I did have an implementation that directly sent the keys events to imgui, without the input dialog, but unfortunately this implementation has many limitations that needed to be implemented manually like clipboard & auto suggestions
With dialog, you have the keyboard with full features
This is how it looks like using the keyboard with and without the dialog box
https://github.com/user-attachments/assets/f8346bdf-a7b3-4e01-95d9-c976451bd2db
ocornut commentedon Jan 10, 2025
You may need to discuss/solve that by getting into precise details (and it would need a separate issue). Right now I don't know if your problems actually required this approach or not.
catlowlevel commentedon Jan 10, 2025
I don't need or want to solve it, which is why i use another implementation (with the dialog box)
Everything's perfect, the only problem i had is this issue here
Fixed IsItemDeactivated(), IsItemDeactivatedAfterEdit() to work when …
TestSuite: added "widgets_status_deactivate_interrupted".
TestSuite: added "widgets_status_deactivate_interrupted".
ocornut commentedon Jan 13, 2025
This should now be fixed by a604d4f
Basic regression test: ocornut/imgui_test_engine@b85d832
Let me know if you run into any other issue or have related questions.
Added ImGuiItemFlags_LiveEdit flag, and much-awaited support for disa…
Split ImGuiItemFlags_LiveEdit into ImGuiItemFlags_LiveEditText, ImGui…