I have the virtual key and scan code for a particular character (in this case 'つ') and I would like to convert it back to the Japanese character. How should I go about doing it? I've searched and tried the below code. However, it's not working... It only works for if the characters are ASCII.
FYI.
- I am only saving the VK/SC and not generating a keypress directly so I can't work with WM_CHAR.
- The 'Z' key on a Japanese keyboard generates 'つ'
Here are the references: C++ Win32: Converting scan code to Unicode character How to translate a virtual-key code to char (depending on locale)? How to convert VK scan codes to appropriate character for language selected How to translate a virtual-key code to char (depending on locale)?
HKL hkl = GetKeyboardLayout(0);
BYTE ks[256] = {};
GetKeyboardState(ks);
uint virtualKeyCode = VkKeyScanEx(L'つ', hkl);
uint scanCode = 44;
uint16_t sc = MapVirtualKeyW(virtualKeyCode, MAPVK_VK_TO_VSC_EX);
const uint32_t flags = 1 << 2; // Do not change keyboard state of this thread
static uint8_t state[256] = { 0 };
state[VK_SHIFT] = false << 7; // Modifiers set the high-order bit when pressed
qDebug() << (unsigned int)'つ';
wchar_t unicodeChar;
if (ToUnicode(virtualKeyCode, sc, state, &unicodeChar, 1, flags) == 1)
qDebug() << QString::fromUtf8(QByteArray(reinterpret_cast<const char*>(&unicodeChar), 2));
unsigned short result = 0;
qDebug() << ToUnicodeEx(virtualKeyCode, scanCode, ks, reinterpret_cast<LPWSTR>(&result), (int)2, (uint)0, hkl);
qDebug() << "TEST" << result;
Output:
14909860 // (unsigned int)'つ';
0 // return value of tounicode()
TEST 0 //result