Skip to content

Commit 0cd3f8c

Browse files
thedmdocornut
authored andcommitted
Backends: Win32: Add support for untranslated key codes
1 parent e6b8cd8 commit 0cd3f8c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

backends/imgui_impl_win32.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct ImGui_ImplWin32_Data
7878
ImGuiMouseCursor LastMouseCursor;
7979
bool HasGamepad;
8080
bool WantUpdateHasGamepad;
81+
bool WantUpdateScancodes;
8182

8283
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
8384
HMODULE XInputDLL;
@@ -102,6 +103,38 @@ static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData()
102103
}
103104

104105
// Functions
106+
static void ImGui_ImplWin32_UpdateScancodes()
107+
{
108+
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
109+
110+
if (!bd->WantUpdateScancodes)
111+
return;
112+
113+
bd->WantUpdateScancodes = false;
114+
115+
ImGuiIO& io = ImGui::GetIO();
116+
117+
for (int i = 0; i < IM_ARRAYSIZE(io.ScancodeMap); ++i)
118+
io.ScancodeMap[i] = -1;
119+
120+
HKL layout = LoadKeyboardLayoutA("00000409", 0); // U.S. English
121+
if (layout == NULL)
122+
return;
123+
124+
for (int i = 0; i < IM_ARRAYSIZE(io.ScancodeMap); ++i)
125+
{
126+
int scancode = io.KeyMap[i] > 0 ? (int)MapVirtualKey(io.KeyMap[i], MAPVK_VK_TO_VSC) : 0;
127+
if (scancode > 0)
128+
{
129+
auto us_vk = MapVirtualKeyExA(scancode, MAPVK_VSC_TO_VK, layout);
130+
if (us_vk > 0)
131+
io.ScancodeMap[i] = us_vk;
132+
}
133+
}
134+
135+
UnloadKeyboardLayout(layout);
136+
}
137+
105138
bool ImGui_ImplWin32_Init(void* hwnd)
106139
{
107140
ImGuiIO& io = ImGui::GetIO();
@@ -119,6 +152,7 @@ bool ImGui_ImplWin32_Init(void* hwnd)
119152
io.BackendPlatformName = "imgui_impl_win32";
120153
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
121154
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
155+
io.BackendFlags |= ImGuiBackednFlags_HasUntranslatedKeys; // We can support mapping keys scancodes into ImGuiKey_XXX (optional, useful in games)
122156

123157
bd->hWnd = (HWND)hwnd;
124158
bd->WantUpdateHasGamepad = true;
@@ -245,6 +279,8 @@ bool ImGui_ImplWin32_Init(void* hwnd)
245279
io.KeyMap[ImGuiKey_F12] = VK_F12;
246280
#endif // IMGUI_HAS_EXTRA_KEYS
247281

282+
ImGui_ImplWin32_UpdateScancodes();
283+
248284
// Dynamically load XInput library
249285
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
250286
const char* xinput_dll_names[] =
@@ -432,6 +468,8 @@ void ImGui_ImplWin32_NewFrame()
432468

433469
// Update game controllers (if enabled and available)
434470
ImGui_ImplWin32_UpdateGamepads();
471+
472+
ImGui_ImplWin32_UpdateScancodes();
435473
}
436474

437475
// Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
@@ -566,6 +604,10 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARA
566604
if ((UINT)wParam == DBT_DEVNODES_CHANGED)
567605
bd->WantUpdateHasGamepad = true;
568606
return 0;
607+
case WM_INPUTLANGCHANGE:
608+
if (io.BackendFlags & ImGuiBackednFlags_HasUntranslatedKeys)
609+
bd->WantUpdateScancodes = true;
610+
return 0;
569611
}
570612
return 0;
571613
}

0 commit comments

Comments
 (0)