From a4f86a541ad24b01e4e1824bb6c43bef82a13812 Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 26 Dec 2022 18:56:07 -0800 Subject: [PATCH] Ensure WM_CHAR characters are parsed as UTF16, then translated to UTF8. --- .../win32_src/pdcurses/wingui/pdcscrn.c | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/3rdparty/win32_src/pdcurses/wingui/pdcscrn.c b/src/3rdparty/win32_src/pdcurses/wingui/pdcscrn.c index 39288e664..6b8bf3b6c 100644 --- a/src/3rdparty/win32_src/pdcurses/wingui/pdcscrn.c +++ b/src/3rdparty/win32_src/pdcurses/wingui/pdcscrn.c @@ -1903,8 +1903,29 @@ static LRESULT ALIGN_STACK CALLBACK WndProc (const HWND hwnd, if( wParam == 3 && !SP->raw_inp) /* Ctrl-C hit */ exit( 0); if( wParam != 9 || !(GetKeyState( VK_SHIFT) & 0x8000)) - if( !key_already_handled || last_key_handled != (int)wParam ) - add_key_to_queue( (int)wParam ); + if (!key_already_handled || last_key_handled != (int)wParam) { +#if _UNICODE + /* if we are building against the unicode runtime, key messages + received by WM_CHAR will be UTF16 encoded; convert it to a + sequence UTF8 bytes and inject them one at a time. + see: https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-char */ + wchar_t utf16[3]; /* may be a surrogate pair */ + utf16[0] = LOWORD(wParam); + utf16[1] = HIWORD(wParam); + utf16[2] = 0; + unsigned char utf8[4 + 4 + 1]; + int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, 0, 0, 0, 0); + if (size > 0 && sizeof(utf8)) { + utf8[0] = 0; + WideCharToMultiByte(CP_UTF8, 0, utf16, -1, utf8, size, 0, 0); + for (int i = 0; i < size - 1; i++) { + add_key_to_queue((int) utf8[i]); + } + } +#else + add_key_to_queue((int)wParam); +#endif + } key_already_handled = FALSE; last_key_handled = 0; return 0;