我想制作一个小应用程序,当按下另一个键(在我的情况下是右侧控制键Right CTRL)时,它将持续模拟UP(↑箭头键)。
然而,我编写的代码每次只会发送一个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;
}