Skip to content

Commit

Permalink
events: use keycodes instead of scancodes
Browse files Browse the repository at this point in the history
this should fix issues related to keyboard layouts
  • Loading branch information
kidanger committed Sep 7, 2023
1 parent 167ae60 commit 6f9c34a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
46 changes: 24 additions & 22 deletions external/imgui/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event)
case SDL_KEYDOWN:
case SDL_KEYUP:
{
int key = event->key.keysym.scancode;
SDL_Keycode key = event->key.keysym.sym & ~SDLK_SCANCODE_MASK;
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
io.KeysDown[key] = (event->type == SDL_KEYDOWN);
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
Expand Down Expand Up @@ -409,27 +409,29 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window, const char* glsl_version)
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)

// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
#define M(k) ((k) & ~SDLK_SCANCODE_MASK)
io.KeyMap[ImGuiKey_Tab] = M(SDLK_TAB);
io.KeyMap[ImGuiKey_LeftArrow] = M(SDLK_LEFT);
io.KeyMap[ImGuiKey_RightArrow] = M(SDLK_RIGHT);
io.KeyMap[ImGuiKey_UpArrow] = M(SDLK_UP);
io.KeyMap[ImGuiKey_DownArrow] = M(SDLK_DOWN);
io.KeyMap[ImGuiKey_PageUp] = M(SDLK_PAGEUP);
io.KeyMap[ImGuiKey_PageDown] = M(SDLK_PAGEDOWN);
io.KeyMap[ImGuiKey_Home] = M(SDLK_HOME);
io.KeyMap[ImGuiKey_End] = M(SDLK_END);
io.KeyMap[ImGuiKey_Insert] = M(SDLK_INSERT);
io.KeyMap[ImGuiKey_Delete] = M(SDLK_DELETE);
io.KeyMap[ImGuiKey_Backspace] = M(SDLK_BACKSPACE);
io.KeyMap[ImGuiKey_Space] = M(SDLK_SPACE);
io.KeyMap[ImGuiKey_Enter] = M(SDLK_RETURN);
io.KeyMap[ImGuiKey_Escape] = M(SDLK_ESCAPE);
io.KeyMap[ImGuiKey_A] = SDLK_a;
io.KeyMap[ImGuiKey_C] = SDLK_c;
io.KeyMap[ImGuiKey_V] = SDLK_v;
io.KeyMap[ImGuiKey_X] = SDLK_x;
io.KeyMap[ImGuiKey_Y] = SDLK_y;
io.KeyMap[ImGuiKey_Z] = SDLK_z;
#undef M

io.SetClipboardTextFn = ImGui_ImplSdlGL3_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplSdlGL3_GetClipboardText;
Expand Down
43 changes: 23 additions & 20 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@

int getCode(const char* name)
{
#define specials(n, sdl, sfml) \
#define specials(n, sdl) \
if (std::string(name) == #n) { \
return SDL_SCANCODE_##sdl; \
return SDLK_##sdl & ~SDLK_SCANCODE_MASK; \
}

specials(left, LEFT, Left);
specials(right, RIGHT, Right);
specials(up, UP, Up);
specials(down, DOWN, Down);
specials(F1, F1, F1);
specials(F2, F2, F2);
specials(F3, F3, F3);
specials(F4, F4, F4);
specials(F5, F5, F5);
specials(F6, F6, F6);
specials(F7, F7, F7);
specials(F8, F8, F8);
specials(F9, F9, F9);
specials(F10, F10, F10);
specials(F11, F11, F11);
specials(F12, F12, F12);
specials(left, LEFT);
specials(right, RIGHT);
specials(up, UP);
specials(down, DOWN);
specials(F1, F1);
specials(F2, F2);
specials(F3, F3);
specials(F4, F4);
specials(F5, F5);
specials(F6, F6);
specials(F7, F7);
specials(F8, F8);
specials(F9, F9);
specials(F10, F10);
specials(F11, F11);
specials(F12, F12);

#undef specials

switch (*name) {
default: {
SDL_Keycode key = SDL_GetKeyFromName(name);
if (key != SDLK_UNKNOWN)
return SDL_GetScancodeFromKey(key);
if (key != SDLK_UNKNOWN) {
key &= ~SDLK_SCANCODE_MASK;
if (key < 512)
return key;
}
}
}
printf("unknown key '%s'\n", name);
Expand Down

0 comments on commit 6f9c34a

Please sign in to comment.