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

Fix ImGuiKey mapping for SDL2 and SDL3 #7670

Closed
wants to merge 2 commits into from
Closed

Conversation

eszdman
Copy link

@eszdman eszdman commented Jun 7, 2024

Since ImGui requires untranslated ImGuiKey values, we should use scancode mapping instead of symbols, as referred to in SDL2 documentation SDL_Keycode

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2024

Thanks for the PR! Where did you see that it require untranslated keys? I think we should explicitly state we try to use translate ones?

@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

Thanks for the PR! Where did you see that it require untranslated keys? I think we should explicitly state we try to use translate ones?

I tried to use it with not English locale settings in my system (arch,KDE), without fix it won't work at all, only chars queue and functional keys
Meanwhile glfw works well

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2024

You are suggesting something different now. Do you mean that you are not receiving any SDL valid key-code from SDL on those keys?
Can you add some printing of all values in the event handler and confirm them?

@ocornut ocornut added the inputs label Jun 8, 2024
@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

You are suggesting something different now. Do you mean that you are not receiving any SDL valid key-code from SDL on those keys? Can you add some printing of all values in the event handler and confirm them?

Sure, I tried it with demo window, Keys down method not working
This problem only appears when starting with another language, after startup with en I can use any layout
I tried to print ImGui_ImplSDL2_KeycodeToImGuiKey argument without fix and get 1094 instead of 119

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2024

Can you add some printing of all values in the event handler and confirm them for a range of keys?

@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

Can you add some printing of all values in the event handler and confirm them for a range of keys?

Yes, I'm pressing QWERTYUIOP:
Startup with us:

keycode: 113:q
keycode: 113:q
keycode: 119:w
keycode: 119:w
keycode: 101:e
keycode: 101:e
keycode: 114:r
keycode: 114:r
keycode: 116:t
keycode: 116:t
keycode: 121:y
keycode: 121:y
keycode: 117:u
keycode: 117:u
keycode: 105:i
keycode: 105:i
keycode: 111:o
keycode: 111:o
keycode: 112:p
keycode: 112:p

Startup with ru:

keycode: 1081:9
keycode: 1081:9
keycode: 1094:F
keycode: 1094:F
keycode: 1091:C
keycode: 1091:C
keycode: 1082::
keycode: 1082::
keycode: 1077:5
keycode: 1077:5
keycode: 1085:=
keycode: 1085:=
keycode: 1075:3
keycode: 1075:3
keycode: 1096:H
keycode: 1096:H
keycode: 1097:I
keycode: 1097:I
keycode: 1079:7
keycode: 1079:7

scancode is the same so I print only SDL_Keycode sym
std::cout<<"keycode: "<<keycode<<":"<<char(keycode)<<std::endl;

@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

Another possible workaround is to try symbols first and then scancode but you will get issues with qwertz layout

            ImGuiKey key = ImGui_ImplSDL2_KeycodeToImGuiKeySym(event->key.keysym.sym);
            if(key == ImGuiKey_None)
                key = ImGui_ImplSDL2_KeycodeToImGuiKey(event->key.keysym.scancode);

Or implement similar method like in glfw backend - ImGui_ImplGlfw_TranslateUntranslatedKey

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2024

First check what those keycode correspond to in SDL mapping.

@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

First check what those keycode correspond to in SDL mapping.

I implemented ImGui_ImplSDL2_TranslateUntranslatedKey it works well but I get error when change locale at runtime
void ImGui::UpdateKeyboardInputs(): Assertion (io.KeysDown[n] == false || IsKeyDown((ImGuiKey)n)) && "Backend needs to either only use io.AddKeyEvent(), either only fill legacy io.KeysDown[] + io.KeyMap[]. Not both!" failed.

Edit: figured it out

@eszdman
Copy link
Author

eszdman commented Jun 8, 2024

Will open new PR with translated solution

@eszdman eszdman closed this Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants