Skip to content

Commit

Permalink
Backends: Win32: Add support for untranslated key codes
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Dec 13, 2021
1 parent 78317c6 commit 72e4a66
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions backends/imgui_impl_win32.cpp
Expand Up @@ -77,6 +77,7 @@ struct ImGui_ImplWin32_Data
ImGuiMouseCursor LastMouseCursor;
bool HasGamepad;
bool WantUpdateHasGamepad;
bool WantUpdateScancodes;

#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
HMODULE XInputDLL;
Expand All @@ -97,6 +98,38 @@ static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData()
}

// Functions
static void ImGui_ImplWin32_UpdateScancodes()
{
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();

if (!bd->WantUpdateScancodes)
return;

bd->WantUpdateScancodes = false;

ImGuiIO& io = ImGui::GetIO();

for (int i = 0; i < IM_ARRAYSIZE(io.ScancodeMap); ++i)
io.ScancodeMap[i] = -1;

HKL layout = LoadKeyboardLayoutA("00000409", 0); // U.S. English
if (layout == NULL)
return;

for (int i = 0; i < IM_ARRAYSIZE(io.ScancodeMap); ++i)
{
int scancode = io.KeyMap[i] > 0 ? (int)MapVirtualKey(io.KeyMap[i], MAPVK_VK_TO_VSC) : 0;
if (scancode > 0)
{
auto us_vk = MapVirtualKeyExA(scancode, MAPVK_VSC_TO_VK, layout);
if (us_vk > 0)
io.ScancodeMap[i] = us_vk;
}
}

UnloadKeyboardLayout(layout);
}

bool ImGui_ImplWin32_Init(void* hwnd)
{
ImGuiIO& io = ImGui::GetIO();
Expand All @@ -114,6 +147,7 @@ bool ImGui_ImplWin32_Init(void* hwnd)
io.BackendPlatformName = "imgui_impl_win32";
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
io.BackendFlags |= ImGuiBackednFlags_HasUntranslatedKeys; // We can support mapping keys scancodes into ImGuiKey_XXX (optional, useful in games)

bd->hWnd = (HWND)hwnd;
bd->WantUpdateHasGamepad = true;
Expand Down Expand Up @@ -240,6 +274,8 @@ bool ImGui_ImplWin32_Init(void* hwnd)
io.KeyMap[ImGuiKey_F12] = VK_F12;
#endif // IMGUI_HAS_EXTRA_KEYS

ImGui_ImplWin32_UpdateScancodes();

// Dynamically load XInput library
#ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
const char* xinput_dll_names[] =
Expand Down Expand Up @@ -427,6 +463,8 @@ void ImGui_ImplWin32_NewFrame()

// Update game controllers (if enabled and available)
ImGui_ImplWin32_UpdateGamepads();

ImGui_ImplWin32_UpdateScancodes();
}

// Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
Expand Down Expand Up @@ -560,6 +598,10 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARA
if ((UINT)wParam == DBT_DEVNODES_CHANGED)
bd->WantUpdateHasGamepad = true;
return 0;
case WM_INPUTLANGCHANGE:
if (io.BackendFlags & ImGuiBackednFlags_HasUntranslatedKeys)
bd->WantUpdateScancodes = true;
return 0;
}
return 0;
}
Expand Down

0 comments on commit 72e4a66

Please sign in to comment.