【发布时间】:2016-11-28 19:03:18
【问题描述】:
我正在尝试获得一个小型应用程序,该应用程序将在按下另一个键时连续模拟 UP(↑ 箭头键),在我的情况下为 Right CTRL。
然而,我编写的代码每次按下只会发送一个 UP - 当我按住 Right CTRL 时,它只会发送一个 UP 并停止。
我想提一下,这段代码完全是根据我在网上找到的文档构建的,我以前从未用 C++ 或任何其他语言编写过任何东西,因此任何建议都会对我有很大帮助。我最初尝试在 CAPS LOCK 处于活动状态时执行此操作,但我发现无论我尝试什么,获取键状态(开/关)都对我不起作用。
int main()
{
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Pause for 1 seconds.
Sleep(1000);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while(1){
if(GetAsyncKeyState(VK_RCONTROL))
{
// Press the "UP arrow" key
ip.ki.wVk = 0x26; // virtual-key code for the "UP arrow" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
Sleep(50);
// Release the "UP arrow" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
Sleep(50);
}
}
// Exit normally
return 0;
}
【问题讨论】: