[英]keybd_event not registering certain keys
我正在创建一个个人项目来帮助我使用自定义 GUI 按下键绑定。 为此,我在外部窗口中运行了一个 DearImGui 框架,作为一个独立的应用程序。
当我按下按钮时,应用程序会为键绑定中的每个键模拟按键。 但是,在发送 control、shift 和 alt 键时似乎存在问题。 我已经为此苦苦挣扎了一天多,现在才意识到这个问题可能是 Windows 中的一项安全措施。 我相信 Windows 阻止我模拟在 control、shift 和 alt 键上的按键,以防止可能伪装成人类的恶意软件。
我决定包含非常简约的示例,因为我相信问题源于 Windows,而不是我的编程错误。
此示例成功按 F8:
if (ImGui::Button("Button 1", ImVec2(334, 30)))
{
keybd_event(0x77, 0, 0, 0); //Press down the Key
keybd_event(0x77, 0, KEYEVENTF_KEYUP, 0); //Release the Key
}
虽然这个应该发送组合 CTRL + F1,但只注册 F1:
if (ImGui::Button("Button 2", ImVec2(334, 30)))
{
keybd_event(0x70, 0, 0, 0); //Press down the Key
keybd_event(VK_CONTROL, 0, 0, 0); //Press down the Key
keybd_event(0x70, 0, KEYEVENTF_KEYUP, 0); //Release the Key
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); //Release the Key
}
注意:所有其他键似乎都可以正常工作,我也尝试过只发送一个 ctrl 键,看看这是否是“你一次只能做一个”的问题(不是。)
您以错误的顺序“按下”键。 您需要在F1键之前“按下” Ctrl键。
if (ImGui::Button("Button 2", ImVec2(334, 30)))
{
keybd_event(VK_CONTROL, 0, 0, 0); //Press down the Key
keybd_event(VK_F1, 0, 0, 0); //Press down the Key
keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0); //Release the Key
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); //Release the Key
}
话虽如此,不推荐使用keybd_event()
,请改用SendInput()
,例如:
if (ImGui::Button("Button 2", ImVec2(334, 30)))
{
INPUT ips[4] = {};
ips[0].type = INPUT_KEYBOARD;
ips[0].ki.wVk = VK_CONTROL;
ips[1] = ip[0];
ips[1].ki.wVk = VK_F1;
ips[2] = ip[1];
ips[2].ki.dwFlags = KEYEVENTF_KEYUP;
ips[3] = ips[0];
ips[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, ips, sizeof(INPUT));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.