pthom / hello_imgui

Hello, Dear ImGui: unleash your creativity in app development and prototyping
https://pthom.github.io/hello_imgui
MIT License
604 stars 91 forks source link

Numpad (keypad) inputs not working on the demos #114

Open quantmage opened 3 weeks ago

quantmage commented 3 weeks ago

Nothing happens if you try typing a digit in any input field with numpad keys. My web app based on hello_imgui has the same issue. And I note that it happens on the demos linked in this repo's readme, too, except the calculator one, which I think doing some manual key input handling.

Not sure if the issue is platform-specific (emscripten+sdl2, I believe) or not. I tried to figure out the cause and spent a few hours this morning looking into the relevant code, but no luck so far.

Another demo of yours (https://github.com/pthom/implot_demo) somehow works with numpad keys, so I wonder what's the difference. It doesn't works 100% though. With Num Lock on, it works. But with Num Lock off, nothing happens (in other words, pressing the keypad 4 doesn't work like the left arrow key.) Another ImGui demo that doesn't use SDL2 (https://jnmaloney.github.io/WebGui/imgui.html) works perfectly with numpad keys for both Num Lock on and off.

I would appreciate any insight on how this can be fixed or any workaround.

pthom commented 2 days ago

Sorry for not answering sooner, I did not see this issue in my feed.

I do not have an numpad on my laptop, and I'm using a Mac and I suspect that this kind of issue is very much dependent on:

Anyhow, this issue is probably linked to Dear ImGui in itself, And it would be worth to have a look on this pinned issue, which deals with the new keyboard API:

https://github.com/ocornut/imgui/issues/4921

And here are some advices on how to run tests: it would be very interesting If you could try to run some diagnostic with ImGui::ShowDemoWindow(). If you open tools/Metric&Debugger/Inputs, you will see:

image

Which shows which events are being sent from the platform backend (sdl, glfw, etc).

That would be a first step in having a better understanding about the issue.


By the way I saw that you are maintaining a fork of Dear ImGui Bundle in which you added some patches on some libraries (Notably I saw something concerning word wrapping). If you have time someday, could you take a few moments to give me a summary of what you did? I'd be interested in studying whether some of those would be interesting to be added to the main repository.

quantmage commented 2 days ago

Appreciate the reply!

Yeah, I agree that the issue is os/backend dependent. I also had to borrow the keyboard of my's son gaming PC to test this. I tested it on both Mac and Linux. On Mac, num lock was not recognized, so it worked as if the num lock is always on (on: number inputs, off: arrow and other control inputs), but at least number inputs from the keypad worked. On Linux, both num lock on & off worked as expected. Maybe the Windows keyboard is not properly recognized for Mac? Here I'm talking about in general applications. ImGui Manual demo didn't work with the keypad at all on both platforms.

But, https://jnmaloney.github.io/WebGui/imgui.html worked like other application on each platform. My understanding is that it uses GLFW backend while ImGui Manual (and imgui_bundle when built for web) uses SDL2. So my guess is the issue lies in the SDL2 backend.

One hack I found out is adding the following code at https://github.com/pthom/imgui/blob/master/backends/imgui_impl_sdl2.cpp#L357:

            if (event->type == SDL_KEYDOWN) {
                if (ImGuiKey_Keypad0 <= key && key <= ImGuiKey_Keypad9) {
                    const char c = '0' + (char)(key - ImGuiKey_Keypad0);
                    io.AddInputCharactersUTF8((const char*)&c);
                } else if (key == ImGuiKey_KeypadDecimal) {
                    const char c = '.';
                    io.AddInputCharactersUTF8((const char*)&c);
                }
            }

Then at least number inputs worked even though the num lock mode is being ignored (regarded as always on.) Maybe there is also a way to check whether the num lock is on or off and forward control inputs instead when it's off... Anyhow this felt like a hack and I didn't pursue further.

I was aware the ImGui's IO event API changes, but https://jnmaloney.github.io/WebGui/imgui.html worked as I noted and it's using 1.89.5, which seems to have the changes.

I already tried tools/Metric&Debugger/Inputs. It showed proper recognitions of keypad inputs, but it still not worked in ImGui Manual demo. For instance, when I press Keypad 1 on Linux (either num lock on or off), it showed:

Screenshot 2024-06-29 at 10 50 22 AM

For the word wrapping, I just patched https://github.com/ocornut/imgui/issues/3237#issuecomment-1435345204. It's not perfect, but good enough for my use cases.