From d27dce58cda087f72418f891c7f95d9ce596fd6d Mon Sep 17 00:00:00 2001 From: ulhc <350246356@qq.com> Date: Fri, 28 Nov 2025 23:05:45 +0800 Subject: [PATCH] Backends: Win32: handle WM_IME_CHAR/WM_IME_COMPOSITION messages to support Unicode inputs on MBCS Windows. (#9099, #3653, #5961) --- backends/imgui_impl_win32.cpp | 19 +++++++++++++++++++ docs/CHANGELOG.txt | 2 ++ 2 files changed, 21 insertions(+) diff --git a/backends/imgui_impl_win32.cpp b/backends/imgui_impl_win32.cpp index b1d715436..5f42466ae 100644 --- a/backends/imgui_impl_win32.cpp +++ b/backends/imgui_impl_win32.cpp @@ -21,6 +21,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2025-12-03: Inputs: handle WM_IME_CHAR/WM_IME_COMPOSITION messages to support Unicode inputs on MBCS (non-Unicode) Windows. (#9099, #3653, #5961) // 2025-10-19: Inputs: Revert previous change to allow for io.ClearInputKeys() on focus-out not losing gamepad state. // 2025-09-23: Inputs: Minor optimization not submitting gamepad input if packet number has not changed. // 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown. @@ -788,6 +789,24 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPA io.AddInputCharacter(wch); } return 0; + case WM_IME_COMPOSITION: + { + // Handling WM_IME_COMPOSITION ensure that WM_IME_CHAR value is correct even for MBCS apps. + // (see #9099, #3653 and https://stackoverflow.com/questions/77450354 topics) + LRESULT result = ::DefWindowProcW(hwnd, msg, wParam, lParam); + return (lParam & GCS_RESULTSTR) ? 1 : result; + } + case WM_IME_CHAR: + if (::IsWindowUnicode(hwnd) == FALSE) + { + if (::IsDBCSLeadByte(HIBYTE(wParam))) + wParam = (WPARAM)MAKEWORD(HIBYTE(wParam), LOBYTE(wParam)); + wchar_t wch = 0; + ::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 2, &wch, 1); + io.AddInputCharacterUTF16(wch); + return 1; + } + return 0; case WM_SETCURSOR: // This is required to restore cursor when transitioning from e.g resize borders to client area. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor(io, bd->LastMouseCursor)) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 058c853d3..14ecb0dd0 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -77,6 +77,8 @@ Other Changes: selects `VkSwapchainCreateInfoKHR`'s `compositeAlpha` value based on `cap.supportedCompositeAlpha`, which seems to be required on some Android devices. (#8784) [@FelixStach] + - Win32: handle WM_IME_CHAR/WM_IME_COMPOSITION to support Unicode inputs on + MBCS (non-Unicode) Windows. (#9099, #3653, #5961) [@ulhc, @ocornut, @Othereum] - Examples: - Win32+DirectX12: ignore seemingly incorrect D3D12_MESSAGE_ID_FENCE_ZERO_WAIT warning on startups on some setups. (#9084, #9093) [@RT2Code, @LeoGautheron]