如果使用KEYEVENTF_EXTENDEDKEY,则KEYEVENTF_KEYUP不起作用

时间:2014-09-09 15:59:31

标签: c++ input keyboard keyevent

我尝试模拟CTRL + NumENTER。我用键盘测试程序(PassMark KeyboardTest)检查了代码,似乎NumENTER被按下但没有被释放。如果我不使用KEYEVENTF_EXTENDEDKEY,一切都会被收集,但我得到正常的返回。

这是我的代码:

INPUT *ip = new INPUT[2];                   // set up input 1

(ip)->type = INPUT_KEYBOARD;                // set up keyboard event
(ip)->ki.wScan = 0;
(ip)->ki.time = 0;
(ip)->ki.dwExtraInfo = 0;

(ip)->ki.wVk = VK_CONTROL;                  // set ip[0] to CTRL
(ip)->ki.dwFlags = 0;

Sleep(100);

(ip+1)->type = INPUT_KEYBOARD;              // set up keyboard event
(ip+1)->ki.wScan = 0;
(ip+1)->ki.time = 0;
(ip+1)->ki.dwExtraInfo = 0;

(ip+1)->ki.wVk = VK_RETURN;                 // set ip[1] to ENTER
(ip+1)->ki.dwFlags = KEYEVENTF_EXTENDEDKEY; // make ENTER to NumENTER

Sleep(100);

SendInput(2, ip, sizeof(INPUT));            // send key

Sleep(100);

(ip)->ki.dwFlags = KEYEVENTF_KEYUP;         // event for keyrelease     
(ip+1)->ki.dwFlags = KEYEVENTF_KEYUP;       // event for keyrelease

Sleep(100);

SendInput(2, ip, sizeof(INPUT));            // release keys

return 0;

1 个答案:

答案 0 :(得分:0)

使用按位或设置dwFlagsKEYEVENTF_EXTENDEDKEYscanCode添加前缀(0xE0(224))。

(ip)->ki.dwFlags |= KEYEVENTF_KEYUP;         // event for keyrelease     
(ip+1)->ki.dwFlags |= KEYEVENTF_KEYUP;       // event for keyrelease