From d23bdae02415ebb35f1bf16fd3e323fa559ff49a Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 26 Dec 2022 17:26:45 -0800 Subject: [PATCH] If _UNICODE is defined, WM_CHAR messages receive UTF16 characters; in this case, we'll convert them to a UTF8 byte sequence and enqueue the characters individually. --- wingui/pdcscrn.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/wingui/pdcscrn.c b/wingui/pdcscrn.c index 67490747..8a6520e4 100644 --- a/wingui/pdcscrn.c +++ b/wingui/pdcscrn.c @@ -1919,7 +1919,28 @@ static LRESULT ALIGN_STACK CALLBACK WndProc (const HWND hwnd, exit( 0); if( wParam != 9 || !(GetKeyState( VK_SHIFT) & 0x8000)) if( !key_already_handled) - add_key_to_queue( (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; return 0;