简体   繁体   English

Win32 - 如何将日文字符的虚拟/扫描码转换回字符?

[英]Win32 - How to convert a Japanese character's virtual/scan code back to the character?

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.但是,它不起作用......它仅适用于字符是 ASCII 的情况。

FYI.供参考。

  1. I am only saving the VK/SC and not generating a keypress directly so I can't work with WM_CHAR.我只保存 VK/SC 而不是直接生成按键,所以我不能使用 WM_CHAR。
  2. The 'Z' key on a Japanese keyboard generates 'つ'日文键盘上的“Z”键生成“つ”

Here are the references: C++ Win32: Converting scan code to Unicode character How to translate a virtual-key code to char (depending on locale)?以下是参考资料: C++ Win32:将扫描代码转换为 Unicode 字符如何将虚拟键代码转换为字符(取决于语言环境)? How to convert VK scan codes to appropriate character for language selected How to translate a virtual-key code to char (depending on locale)? 如何将 VK 扫描代码转换为所选语言的适当字符如何将虚拟键代码转换为字符(取决于语言环境)?

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

It seems there is an IME rather than keyboard layout for Japanese in Windows. Windows 中似乎有一个 IME 而不是日语的键盘布局。 在此处输入图像描述

Edit: Message from Windows SDK Team in Japan is The KANA character is handled by IME, IME converts it from the scan code.编辑:来自日本 Windows SDK 团队的消息是假名字符由 IME 处理,IME 将其从扫描码转换。 There is no Win32 API to do this conversion, if the customer wants to convert the KANA character from a scan code, they will need to build their own table to convert it themselves.没有Win32 API来做这个转换,如果客户想从扫描码转换假名字符,他们需要自己建表来转换它。

IME is involved in this case.本案涉及 IME。 ToUnicode is working with usual keyboard layouts (that have SC/VK/CHAR tables inside their dlls). ToUnicode使用通常的键盘布局(它们的 dll 中有 SC/VK/CHAR 表)。

AFAIK in Japanese case English keyboard layout is used with Japanese IME on top of it - thats why you cannot simply use ToUnicode to produce Japansese chars. AFAIK 在日文的情况下,英文键盘布局与日文 IME 一起使用 - 这就是为什么您不能简单地使用ToUnicode来生成日文字符。

On the other side - IME injects WM_CHAR messages into your window's message queue after 'TranslateMessage' call.另一方面 - 在“TranslateMessage”调用之后,IME 将WM_CHAR消息注入到窗口的消息队列中。 IMEs can be a quite complex. IME 可能相当复杂。 They can produce characters from different sources - keydown, voice, char etc.他们可以从不同的来源生成字符——keydown、voice、char 等。

Proper way is to process WM_CHAR and do not try to do deal with IME APIs by yourself...正确的方法是处理WM_CHAR并且不要尝试自己处理 IME API...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM