@@ -78,6 +78,7 @@ struct ImGui_ImplWin32_Data
78
78
ImGuiMouseCursor LastMouseCursor;
79
79
bool HasGamepad;
80
80
bool WantUpdateHasGamepad;
81
+ bool WantUpdateScancodes;
81
82
82
83
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
83
84
HMODULE XInputDLL;
@@ -102,6 +103,38 @@ static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData()
102
103
}
103
104
104
105
// 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
+
105
138
bool ImGui_ImplWin32_Init (void * hwnd)
106
139
{
107
140
ImGuiIO& io = ImGui::GetIO ();
@@ -119,6 +152,7 @@ bool ImGui_ImplWin32_Init(void* hwnd)
119
152
io.BackendPlatformName = " imgui_impl_win32" ;
120
153
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
121
154
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)
122
156
123
157
bd->hWnd = (HWND)hwnd;
124
158
bd->WantUpdateHasGamepad = true ;
@@ -245,6 +279,8 @@ bool ImGui_ImplWin32_Init(void* hwnd)
245
279
io.KeyMap [ImGuiKey_F12] = VK_F12;
246
280
#endif // IMGUI_HAS_EXTRA_KEYS
247
281
282
+ ImGui_ImplWin32_UpdateScancodes ();
283
+
248
284
// Dynamically load XInput library
249
285
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
250
286
const char * xinput_dll_names[] =
@@ -432,6 +468,8 @@ void ImGui_ImplWin32_NewFrame()
432
468
433
469
// Update game controllers (if enabled and available)
434
470
ImGui_ImplWin32_UpdateGamepads ();
471
+
472
+ ImGui_ImplWin32_UpdateScancodes ();
435
473
}
436
474
437
475
// 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
566
604
if ((UINT)wParam == DBT_DEVNODES_CHANGED)
567
605
bd->WantUpdateHasGamepad = true ;
568
606
return 0 ;
607
+ case WM_INPUTLANGCHANGE:
608
+ if (io.BackendFlags & ImGuiBackednFlags_HasUntranslatedKeys)
609
+ bd->WantUpdateScancodes = true ;
610
+ return 0 ;
569
611
}
570
612
return 0 ;
571
613
}
0 commit comments