[英]Reading UTF-8 file, passing contents to other application using SendInput
I have a utf-8 formatted file that contains a character ' ů
', when I read it in c++ using ifstream and cout it, the output is ' ů
'.我有一个 utf-8 格式的文件,其中包含一个字符“ ů
”,当我使用 ifstream 在 c++ 中读取它并对其进行 cout 时,输出为“ ┼»
”。 I can solve it by adding ' SetConsoleOutputCP( 65001 );
我可以通过添加 ' SetConsoleOutputCP( 65001 );
来解决它SetConsoleOutputCP( 65001 );
', afterwards Im sending the char using ' SendInput();
',然后我使用' SendInput();
发送字符SendInput();
' to another window. '到另一个窗口。 But because the SetConsoleOutput() doesnt affect SendInput() the application recieves the corrupted character ' ┼»
'.但是因为 SetConsoleOutput() 不影响 SendInput() 应用程序收到损坏的字符 ' ┼»
'。 The question is how to SendInput() accented character?问题是如何使用SendInput() 重音字符?
ifstream file(path);
string str;
while (getline (file, str))
{
cout << endl << str <<endl;
for (char &c : str) {
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.dwFlags = KEYEVENTF_UNICODE;
if (isupper(c)) {
ip.ki.wVk = VK_LSHIFT;
SendInput(1, &ip, sizeof(INPUT));
} else {
ip.ki.dwFlags = KEYEVENTF_KEYUP;
ip.ki.wVk = VK_LSHIFT;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_UNICODE;
}
ip.ki.wVk = VkKeyScanA(c);
SendInput(1, &ip, sizeof(INPUT));
Sleep(100);
}
INPUT enter;
enter.type = INPUT_KEYBOARD;
enter.ki.wScan = 0;
enter.ki.time = 0;
enter.ki.dwExtraInfo = 0;
enter.ki.dwFlags = 0;
enter.ki.wVk = VK_RETURN;
SendInput(1, &enter, sizeof(INPUT));
cout << "enter pressed";
}
UTF-8 and Windows are not good friends. UTF-8 和 Windows 不是好朋友。 Almost everything you are going to do with Windows requires UTF-16.几乎所有你要在 Windows 上做的事情都需要 UTF-16。 You need to convert your UTF-8 string to UTF-16.您需要将 UTF-8 字符串转换为 UTF-16。 On Windows, that would be "wstring", but you should use the right conversion routine.在 Windows 上,这将是“wstring”,但您应该使用正确的转换例程。 Alas the C++ standard cannot be bothered to give you a working one one right now, so Windows-specific MultiByteToWideChar
seems like a good option.唉,C++ 标准现在无法为您提供一个可用的标准,因此特定于 Windows 的MultiByteToWideChar
似乎是一个不错的选择。
ip.ki.dwFlags = KEYEVENTF_UNICODE; if (isupper(c)) { ip.ki.wVk = VK_LSHIFT;
It doesn't work this way.这种方式行不通。
If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.如果 dwFlags 成员指定 KEYEVENTF_UNICODE,则 wVk 必须为 0。
Unicode characters cannot be combined with Shift or other modifiers. Unicode 字符不能与 Shift 或其他修饰符组合使用。 They are not key codes, they are characters.它们不是关键代码,而是字符。 A
and a
are different characters, there is no need to send shift presses to differentiate between them. A
和a
是不同的字符,不需要发送 shift press 来区分它们。 You also don't need VkKeyScanA
, because you want to send Unicode characters, not scan codes.您也不需要VkKeyScanA
,因为您想发送 Unicode 字符,而不是扫描码。
You need to convert the UTF-8 data to UTF-16 first, such as with MultiByteToWideChar()
or equivalent, and then you can pass the individual UTF-16 codeunits to SendInput()
using the KEYEVENTF_UNICODE
flag.您需要先将 UTF-8 数据转换为 UTF-16,例如使用MultiByteToWideChar()
或等效方法,然后您可以使用KEYEVENTF_UNICODE
标志将各个 UTF-16 代码单元传递给SendInput()
。 See my answer to Sending Two or more chars using SendInput for an example of sending UTF-16 strings with SendInput()
.有关使用SendInput()
发送 UTF-16 字符串的示例,请参阅我对使用 SendInput 发送两个或多个字符的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.