我使用 GetAsyncKeyState() 获取输入,然后使用 ToUnicodeEx() 将其转换为 unicode:
wchar_t character[1];
ToUnicodeEx(i, scanCode, keyboardState, character, 1, 0, layout);
我可以使用 wfstream 将其写入文件,如下所示:
wchar_t buffer[128]; // Will not print unicode without these 2 lines
file.rdbuf()->pubsetbuf(buffer, 128);
file.put(0xFEFF); // BOM needed since it's encoded using UCS-2 LE
file << character[0];
当我在 Notepad++ 中打开此文件时,它位于 UCS-2 LE 中,而我希望它采用 UTF-8 格式。我相信 ToUnicodeEx() 以 UCS-2 LE 格式返回它,它也只适用于宽字符。有没有办法通过首先转换为 UTF-8 来使用 fstream 或 wfstream 来做到这一点?谢谢!