简体   繁体   English

keybd_event显示奇怪的行为

[英]keybd_event showing weird behavior

I am trying to simulate enter key press using following code: 我正在尝试使用以下代码模拟Enter键:

keybd_event(VK_RETURN , 0, 0, 0);
keybd_event(VK_RETURN , 0, KEYEVENTF_KEYUP, 0);

It works well in my 32 bit Windows XP OS but when I try in Windows 8 x64 machine, spacebar key press is simulated instead of enter key. 它在我的32位Windows XP操作系统上运行良好,但是当我在Windows 8 x64计算机上尝试时,模拟空格键而不是回车键。

What should I do to get correct result? 我应该怎么做才能得到正确的结果?

I also tried using SendInput as shown below but i face the same problem. 我也尝试使用SendInput,如下所示,但我遇到了同样的问题。

void typeKey(short virtualKey)
{

 INPUT ip;
 ip.type = INPUT_KEYBOARD;
 ip.ki.wScan = 0; 
 ip.ki.time = 0;
 ip.ki.dwExtraInfo = 0;
 ip.ki.wVk = virtualKey; // virtual-key code for the character
 ip.ki.dwFlags = 0;
 SendInput(1, &ip, sizeof(INPUT));
 ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
 SendInput(1, &ip, sizeof(INPUT));
}

//function call
typeKey(VK_RETURN);

Note: I am compiling a 32 bit executable on XP machine and using the same executable on Windows 8. 注意:我正在XP机器上编译32位可执行文件,并在Windows 8上使用相同的可执行文件。

This is just an educated guess, but your old function uses argument types that differ in length from x86 to x64. 这只是一个有根据的猜测,但是您的旧函数使用的参数类型的长度在x86到x64之间不同。 That may be a problem. 那可能是个问题。 Listen to the documentation that says: 听文档说:

Note This function has been superseded. 注意此功能已被取代。 Use SendInput instead. 请改用SendInput

I confirm your observation on 64-bit Windows 8.1. 我确认您对64位Windows 8.1的观察。 For some reason, VK_RETURN does not work correctly with SendInput() . 由于某些原因,VK_RETURN无法与SendInput()一起正常使用。

The solution seems to be to supply the scan code as well like this: 解决方案似乎是提供扫描代码,如下所示:

ip.ki.wScan = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM