The following program didn't do anything, athough had been expected to simulate pressing "a" and "b" for each second. Why it doesn't work?
#include <Windows.h>
#include <iostream>
using namespace std;
const int INPUTS = 4;
int main()
{
INPUT inputArray[INPUTS];
INPUT input;
input.type = INPUT_KEYBOARD;
//Press 'a' key
input.ki.wVk = 0x41;
input.ki.wScan = MapVirtualKey(0x41,MAPVK_VK_TO_VSC);
inputArray[0] = input;
//Release 'a' key
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputArray[1] = input;
//Press 'b' key
input.ki.dwFlags = 0;
input.ki.wVk = 0x42;
input.ki.wScan = MapVirtualKey(0x42,MAPVK_VK_TO_VSC);
inputArray[2] = input;
//Release 'b' key
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputArray[3] = input;
Sleep(5000);
std::cout<<"GO!!!
";
for(int i=0; i<100; i++)
{
SendInput(sizeof(inputArray),inputArray,sizeof(INPUT));
Sleep(1000); //Don't remove!
}
std::cout<<GetLastError()<<std::endl;
system("Pause");
return 0;
}
The last error is
ERROR_NOACCESS
998 (0x3E6)
Invalid access to memory location.
but I don't know what caused it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…