【问题标题】:special characters keybd_event c++特殊字符keybd_event c++
【发布时间】:2016-02-14 20:16:30
【问题描述】:

我有一个带有特殊字符的字符串,比如std::string test = "hello é -"; 我想用keybd_event 写这个字符串,但是特殊字符的显示效果不好。 我这样做:

std::string result = "Hello é -";
int taille = result.size ();

for (int i = 0 ; i < taille ; i++)
{
    keybd_event(VkKeyScan(result.at(i)),0x9e,0 , 0);
    keybd_event(VkKeyScan(result.at(i)),0x9e, KEYEVENTF_KEYUP,0);
}

我做了一些研究,发现 std::wstring 用于 unicode,但即使这样它也不起作用,知道吗?

【问题讨论】:

  • 问问自己如何在键盘上键入任意字符。然后重现那个。更好的是,我们自动化。
  • 直接来自keybd_event 的文档:"此功能已被取代。请改用SendInput。" 发送一个不能与键盘穿插的击键序列来自其他来源的输入(如您的问题中的情况)是原因,为什么 keybd_event 已被替换为 SendInput
  • 是的,我阅读了有关 SendInput 的文档,但我不太了解如何将特殊字符发送到 SendInput @IInspectable
  • 我发现我发送 unicode hexa KEYBDINPUT
  • @simon 你不明白 IInspectable 在说什么。这两个问题是无关的。你应该听从这个建议。

标签: c++ string winapi unicode keyboard


【解决方案1】:

你需要使用SendInput()而不是keybd_event(),例如:

std::wstring result = L"Hello é -";
int taille = result.size ();

std::vector<INPUT> inputs(taille);

for (int i = 0; i < taille; ++i)
{
    INPUT &input = inputs[i];
    input.type = INPUT_KEYBOARD;
    input.ki.wVk = 0;
    input.ki.wScan = result.at(i);
    input.ki.dwFlags = KEYEVENTF_UNICODE;
    input.ki.time = 0;
    input.ki.dwExtraInfo = GetMessageExtraInfo();
}

SendInput(inputs.size(), &inputs[0], sizeof(INPUT));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多