ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.73k stars 10.17k forks source link

InputText/InputTextMultiline doesn't detect enter/new line/carriage return using other input device #2113

Open zivler opened 5 years ago

zivler commented 5 years ago

Version/Branch of Dear ImGui: 1.63 Back-end file/Renderer/OS: Back-ends: imgui_impl_sd2.cpp + imgui_impl_opengl2.cpp OS: Windows 10 32-bit Compiler: MinGW

My Issue/Question: Hello. I am trying out imgui's features but haven't tried making a project yet and i am quite new. Just studying the demo from the sample binaries. I would like to try something that use other input device (like a barcode scanner) for the input. In the "Console" example, I have tried using a scanner for the input but it doesn't detect the carriage return. I have also tried the InputTextMultiline but the same. Is there a way to do this without so much modification on the imgui code?

Standalone, minimal, complete and verifiable example: N/A

ocornut commented 5 years ago

Hello @zivler ,

Your question is ambiguous and ill-defined. We have no idea what your barcode system is, how it works and how it is expected to work. Dear imgui takes inputs via the io.AddInputCharacter() and io.KeysDown[], you can follow how they are set up in imgui_impl_sdl2.cpp. Assuming your barcode system is outputting keyboard events you may log the events from SDL and see how they translate or not to imgui events.

ocornut commented 5 years ago

There is the possibility that it is outputting a "NumPad Return" key down event instead of a regular one. In this case you may merge https://github.com/ocornut/imgui/pull/2005/files and wire it in SDL. If that is indeed your issue let us know, that would be an extra case for merging #2005.

zivler commented 5 years ago

Hi Omar.Thank you for the response.Sorry if my issue is not that clear.From what i understand from the code (correct me if i'm wrong), the InputText flag "ImGuiInputTextFlags_EnterReturnsTrue" would add a carriage return \r and/or the new line \n to the array of characters when the enter key (or the assigned key) is pressed. This would not be a problem when we are using a regular keyboard since we can always assign any key to "Enter" whether it's in the numeric. In the case of other input device (where in my case I have tried the barcode scanner to enter the text), the device would pass a string together with the carriage return and new line to it so when it is read into a standard input stream it would automatically go to a new line (\r\n). I am not sure why the InputText and the InputTextMultiline would only read the valid array of characters and skip the escape sequence \r\n. So in this case when i tried the InputTextMultiline and use the barcode scanner to read a barcode, it would not move to the next line but I have no issue when I tried it on for example 'Notepad' or 'Excel'.

ocornut commented 5 years ago

the InputText flag "ImGuiInputTextFlags_EnterReturnsTrue" would add a carriage return \r and/or the new line \n to the array of characters when the enter key (or the assigned key) is pressed.

The description for that flag says "Return 'true' when Enter is pressed (as opposed to when the value was modified)", so that is incorrect.

I am not sure why the InputText and the InputTextMultiline would only read the valid array of characters and skip the escape sequence \r\n. So in this case when i tried the InputTextMultiline and use the barcode scanner to read a barcode,

This is because our input handling mechanism is using the Enter KEY to trigger a new line, and filtering the \r\n Characters. There is a distinction between keys and characters in most systems (including Windows).

Because we handle many options and code-paths (with keyboard modifiers etc.) it is more natural for us to be checking the keys here. I assume your scanner only output characters.

If your scanner provide an instantaneous character output one ugly workaround would be to do:

    case WM_CHAR:
        if (wParam == '\r')
        {
            wParam = '\n';
            io.KeysDown[ImGuiKey_Enter] = false;
        }

to replace the characters 13 ('\r') with characters 10 ('\n') when passing them to io.AddInputCharacter() and cancelling the corresponding key-press (if any). At least you'd only be modifying imgui_impl_win32.cpp here.

zivler commented 5 years ago

Thanks.I tried that but still the same (i.e. can not go to new line/detect carriage). I think the 'InputText' skips the delimiters and just focus on handling key presses. What I can think of is just having a condition that the input stream from the barcode scanner will have 10 or more characters (for example) then sending '\n' when condition is fulfilled: ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), ... if ((InputBuf != NULL && strlen(InputBuf)>=10)... But this is just ugly (>.<) and not flexible solution. How and where do I add the condition that the when '\r' is detected in the 'InputBuf' it will automatically perform a new line '\n'?

ocornut commented 5 years ago

You can setup InputText callbacks to replace and add characters.

On 6 Oct 2018, at 10:15, zivler notifications@github.com wrote:

Thanks.I tried that but still the same (i.e. can not go to new line/detect carriage). I think the 'InputText' skips the delimiters and just focus on handling key presses. What I can think of is just having a condition that the input stream from the barcode scanner will have 10 or more characters (for example) then sending '\n' when condition is fulfilled: ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), ... if ((InputBuf != NULL && strlen(InputBuf)>=10)... But this is just ugly (>.<) and not flexible solution. How and where do I add the condition that the when '\r' is detected in the 'InputBuf' it will automatically perform a new line '\n'?

ā€” You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

zivler commented 5 years ago

thanks for the tip.i might need to study again until i grasp the structure..i tried again and realized the inputtext is reading the ā€˜\r\nā€™ from the barcode scanner but not that consistent.it might be the input stream is filtered right away before detecting the escape sequence..and trying to put a delay will have noticeable lag in the overall program.