简体   繁体   English

C ++ SendInput不放开键

[英]C++ SendInput not letting go off the key

I use these codes below, they keep holding both the left and right keys and never let go off them until I press those keys myself. 我在下面使用这些代码,它们一直按住左右键,在我自己按下这些键之前,不要松开它们。

I could run the left key code and keep pressing the right key.. and it will keep pressing the left key.. only until I press the left key myself will it stop. 我可以运行左键代码并持续按右键,然后它将一直按左键..直到我自己按下左键,它才会停止。

Same thing happens for the right key when I run the right key code it keeps pressing right I could press left key and it will still keep pressing right key until I press right key myself. 当我运行右键代码时,右键也会发生同样的事情,它会一直按向右键,而我可以一直按向左键,直到我自己按下向右键时,它仍然会一直按向右键。

Reason why I used SendInput is because keybd_event is unreliable.. if you don't put a Sleep(Milliseconds) in between DOWN/UP it won't even do anything, and using a Sleep() will throw of the intent of this program it has to tap it as fast as possible, holding the key for any amount of time could result in the wrong answer (this is like a aiming program) 我之所以使用SendInput是因为keybd_event不可靠..如果您在DOWN / UP之间不放置Sleep(毫秒),它甚至什么也不会做,而使用Sleep()会抛出该程序的意图。它必须尽可能快地点击它,在任何时候按住键都可能导致错误的答案(这就像一个瞄准程序)

This one is for tapping the Right Key 这是用于点击右键

INPUT ip[1];
ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wScan = 0;
ip[0].ki.time = 0;
ip[0].ki.dwExtraInfo = 0;
ip[0].ki.wVk = VK_RIGHT; 
ip[0].ki.dwFlags = 0;

ip[1].type = INPUT_KEYBOARD;
ip[1].ki.wScan = 0;
ip[1].ki.time = 0;
ip[1].ki.dwExtraInfo = 0;
ip[1].ki.wVk = VK_RIGHT; 
ip[1].ki.dwFlags = KEYEVENTF_KEYUP;

SendInput(2, ip, sizeof(INPUT));

This one is for tapping the Left Key 这是用于点击向左键

INPUT ip[1];
ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wScan = 0;
ip[0].ki.time = 0;
ip[0].ki.dwExtraInfo = 0;
ip[0].ki.wVk = VK_LEFT; 
ip[0].ki.dwFlags = 0;

ip[1].type = INPUT_KEYBOARD;
ip[1].ki.wScan = 0;
ip[1].ki.time = 0;
ip[1].ki.dwExtraInfo = 0;
ip[1].ki.wVk = VK_LEFT; 
ip[1].ki.dwFlags = KEYEVENTF_KEYUP;

SendInput(2, ip, sizeof(INPUT));

Edit: New code looks like this, err don't like this style. 编辑:新代码看起来像这样,错误的不喜欢这种风格。

INPUT ip[1] = {0};
ip[0].type = ip[1].type = INPUT_KEYBOARD;
ip[0].ki.wScan = ip[1].ki.wScan = 0;
ip[0].ki.time = ip[1].ki.time = 0;
ip[0].ki.dwExtraInfo = ip[1].ki.dwExtraInfo = 0;
ip[0].ki.wVk = ip[1].ki.wVk = VK_LEFT; 
ip[0].ki.dwFlags = 0;
ip[1].ki.dwFlags = KEYEVENTF_KEYUP;

SendInput(2, ip, sizeof(INPUT));

Edit again: (also doesn't work) I attempted to trick it into sending KEYUP first. 再次编辑:(同样不起作用)我试图欺骗它,使其首先发送KEYUP。

int intRetValue = -1;
INPUT ip[2] = {0};

ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wScan = 0;
ip[0].ki.time = 0;
ip[0].ki.dwExtraInfo = GetMessageExtraInfo();
ip[0].ki.wVk = VK_RIGHT; 
ip[0].ki.dwFlags = KEYEVENTF_KEYUP;
ZeroMemory(&ip[1], sizeof(INPUT));
ip[1].type = INPUT_KEYBOARD;
ip[1].ki.wScan = 0;
ip[1].ki.time = 0;
ip[1].ki.dwExtraInfo = GetMessageExtraInfo();
ip[1].ki.wVk = VK_RIGHT; 
ip[1].ki.dwFlags = 0;
ZeroMemory(&ip[2], sizeof(INPUT));
ip[2].type = INPUT_KEYBOARD;
ip[2].ki.wScan = 0;
ip[2].ki.time = 0;
ip[2].ki.dwExtraInfo = GetMessageExtraInfo();
ip[2].ki.wVk = VK_RIGHT; 
ip[2].ki.dwFlags = KEYEVENTF_KEYUP;

intRetValue = SendInput(3, ip, sizeof(INPUT));
printf("retValue = %d\n", intRetValue);

Figured it out. 弄清楚了。

Can't use SendInput chained and without delays. 不能连续使用SendInput且没有延迟。

Need Sleep(Milliseconds); 需要Sleep(Milliseconds); in between each other. 在彼此之间。

INPUT ip = {0};
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = GetMessageExtraInfo();
ip.ki.wVk = VK_RIGHT; 
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));

Sleep(50); //Figure out the proper timing here

INPUT ip2 = {0};
ip2.type = INPUT_KEYBOARD;
ip2.ki.wScan = 0;
ip2.ki.time = 0;
ip2.ki.dwExtraInfo = GetMessageExtraInfo();
ip2.ki.wVk = VK_RIGHT; 
ip2.ki.dwFlags = 2;
SendInput(1, &ip2, sizeof(INPUT));

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

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