SendInput and GetAsyncKeyState

Hello, I'm trying to do something simple: I want to press a key on the keyboard like "F1" or "Escape" and have the program press down a series of keys like "abcd". I get the basic syntax for GetAsyncKeyState, can emulate mouse movements with mouse_event but don't know how to emulate keyboard events.

The msdn page says the following:

1
2
3
4
5
UINT WINAPI SendInput(
  _In_  UINT nInputs,
  _In_  LPINPUT pInputs,
  _In_  int cbSize
);


This is what I'm using to call it:

1
2
3
4
if(GetAsyncKeyState(VK_F3))
{
SendInput(..,..,..) { } //Not sure how to use this
}


EDIT: I figured it out. I will post the code shortly in case someone's looking for something similar to this.
Last edited on
Here's the basic code structure, I don't quite understand it all yet but it should be useful for anyone wanting to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void pressD();

int main()
{
    while (true)
   {
      if (GetAsyncKeyState(VK_F1))
      {
         pressD();
      }
      if (GetAsyncKeyState(VK_ESCAPE))
         break;
   }
return 0;
}

void pressD()
{
   INPUT ip;

   ip.type = INPUT_KEYBOARD;
   ip.ki.wScan = 0;
   ip.ki.time = 0;
   ip.ki.dwExtraInfo = 0;

   ip.ki.wVk = 0x44; //Hex code for 'D'
   ip.ki.dwFlags = 0; //Press the key down ?
   SendInput(1, &ip, sizeof(INPUT)); //Use function

   Sleep(50); //Sleep so it doesn't spam the key press

   ip.ki.dwFlags = KEYEVENTF_KEYUP; //Release the key
   SendInput(1, &ip, sizeof(INPUT)); //Use function
}
Last edited on
Topic archived. No new replies allowed.