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)

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.

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.

Need 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归因
不隶属于 StackOverflow
scroll top