Commit e899f77f authored by mercury233's avatar mercury233

use ToUnicodeEx instead of ToAsciiEx

parent f07e86dd
...@@ -833,33 +833,48 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) ...@@ -833,33 +833,48 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
GetKeyboardState(allKeys); GetKeyboardState(allKeys);
// Ensure current key state reflects this message.
if (wParam < 256)
{
if (event.KeyInput.PressedDown)
allKeys[wParam] |= 0x80;
else
allKeys[wParam] &= ~0x80;
}
event.KeyInput.Shift = ((allKeys[VK_SHIFT] & 0x80)!=0); event.KeyInput.Shift = ((allKeys[VK_SHIFT] & 0x80)!=0);
event.KeyInput.Control = ((allKeys[VK_CONTROL] & 0x80)!=0); event.KeyInput.Control = ((allKeys[VK_CONTROL] & 0x80)!=0);
// Handle unicode and deadkeys in a way that works since Windows 95 and nt4.0 // Translate to Unicode using the active keyboard layout.
// Using ToUnicode instead would be shorter, but would to my knowledge not run on 95 and 98. // We intentionally don't emit characters on key-up events.
WORD keyChars[2]; if (event.KeyInput.PressedDown)
UINT scanCode = HIWORD(lParam); {
int conversionResult = ToAsciiEx(wParam,scanCode,allKeys,keyChars,0,KEYBOARD_INPUT_HKL); WCHAR unicodeBuf[8];
if (conversionResult == 1) unicodeBuf[0] = 0;
{ UINT scanCode = (UINT)((lParam >> 16) & 0xFF);
// ToAsciiEx writes translated ANSI chars into WORDs. Convert only the int conversionResult = ToUnicodeEx((UINT)wParam, scanCode, allKeys, unicodeBuf,
// produced byte(s) instead of interpreting the whole WORD array as a byte buffer. (int)(sizeof(unicodeBuf) / sizeof(unicodeBuf[0])), 0, KEYBOARD_INPUT_HKL);
char bytes[2];
bytes[0] = static_cast<char>(keyChars[0] & 0xFF); if (conversionResult == -1)
bytes[1] = static_cast<char>(keyChars[1] & 0xFF); {
WORD unicodeChar = 0; // Dead-key: clear the keyboard layout state and do not emit a character.
MultiByteToWideChar( ToUnicodeEx((UINT)wParam, scanCode, allKeys, unicodeBuf,
KEYBOARD_INPUT_CODEPAGE, (int)(sizeof(unicodeBuf) / sizeof(unicodeBuf[0])), 0, KEYBOARD_INPUT_HKL);
MB_PRECOMPOSED, // default event.KeyInput.Char = 0;
bytes, }
1, else if (conversionResult > 0)
(WCHAR*)&unicodeChar, {
1 ); event.KeyInput.Char = unicodeBuf[0];
event.KeyInput.Char = unicodeChar;
} }
else else
{
event.KeyInput.Char = 0; event.KeyInput.Char = 0;
}
}
else
{
event.KeyInput.Char = 0;
}
// allow composing characters like '@' with Alt Gr on non-US keyboards // allow composing characters like '@' with Alt Gr on non-US keyboards
if ((allKeys[VK_MENU] & 0x80) != 0) if ((allKeys[VK_MENU] & 0x80) != 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment