As @David Heffernan said that, SendInput can do this.
This is the least code.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
HWND hwnd;
INPUT input[2];
memset(input, 0, sizeof(input));
if (!(hwnd = FindWindow(L"AAAA", NULL)))
{
cout << "Couldn't find window";
}
else
{
Sleep(5000);
input[0].type = input[1].type = INPUT_KEYBOARD;
SetForegroundWindow(hwnd);
input[0].ki.wVk = 'W';
input[0].ki.dwFlags = 0;
input[1].ki.wVk = 'W';
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
}
return 0;
}
Put the key down and key up events into an array of length 2 and send them as a
single atomic unit.
Set the value of ki.dwFlags
to control up and down states of the key.