This program is a simple example of using the Win32 SendInput function to generate a simulated keystroke. When you run this program, it simply waits 5 seconds and then simulates a press and release of the “A” key on the keyboard.
Here’s the complete source code.
//
// keystroke.c - Pauses, then simulates a key press
// and release of the "A" key.
//
// Written by Ted Burke - last updated 17-4-2012
//
// To compile with MinGW:
//
// gcc -o keystroke.exe keystroke.c
//
// To run the program:
//
// keystroke.exe
//
// ...then switch to e.g. a Notepad window and wait
// 5 seconds for the A key to be magically pressed.
//
// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
#include <windows.h>
int main()
{
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Pause for 5 seconds.
Sleep(5000);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the "A" key
ip.ki.wVk = 0x41; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
// Exit normally
return 0;
}
Here’s a screenshot of the console window where I compiled and ran the program.
As soon as I ran the program in the console window, I switched the focus to a Notepad window where the simulated keystroke typed a character, as shown in the following screenshot.



where can I find the different key codes for different buttons? like 0×41, but for other keys
Hi Bram,
Here’s a link to the list of virtual key codes on Microsoft’s MSDN website.
There is an alternative way of filling the keyboard event structure where you specify the key as a unicode character rather than as a virtual key code. For example, you could simulate pressing the ‘d’ key, with something like the following:
INPUT ip; ip.type = INPUT_KEYBOARD; ip.ki.time = 0; ip.ki.dwFlags = KEYEVENTF_UNICODE; // Specify the key as a unicode character ip.ki.wScan = 'd'; // Which keypress to simulate ip.ki.wVk = 0; ip.ki.dwExtraInfo = 0; SendInput(1, &ip, sizeof(INPUT));I think you still need to use virtual key codes sometimes though to simulate the special keys on the keyboard, so it’s best to know both ways.
thanks for the link!
However, when I try to compile, I get errors that both INPUT and ip are undeclared.
D:\eclipse workspace>gcc -o keystroke.exe keystroke.c
keystroke.c: In function `main’:
keystroke.c:25: `INPUT’ undeclared (first use in this function)
keystroke.c:25: (Each undeclared identifier is reported only once
keystroke.c:25: for each function it appears in.)
keystroke.c:25: parse error before `ip’
keystroke.c:32: `ip’ undeclared (first use in this function)
keystroke.c:32: `INPUT_KEYBOARD’ undeclared (first use in this function)
do I need to compile with a VS compiler?
No, you shouldn’t need to use the VC++ compiler. I’m using gcc myself. It’s the version installed with MinGW. Are you using MinGW too?
Did you definitely include both of the following lines at the start of your program?
#define WINVER 0x0500 #include <windows.h>The SendInput function (and some related stuff) is defined in a header file that gets included by windows.h. However, the definitions are dependent on the versions of Windows that your compiling for. That’s why the WINVER definition line must come before windows.h is inluded. Basically, it makes sure that windows.h will include all the relevant stuff. If you don’t set WINVER before including windows.h, gcc thinks you want to compile a program that will also be compatible wiith older versions of Windows that didn’t support SendInput etc.
If that’s not the solution, let me know and I’ll see if I can work out why it’s not working for you.
Here’s a complete main.c example using the second (unicode) form of SendInput:
#define WINVER 0x0500 #include <windows.h> int main() { // Pause for 5 seconds. Sleep(5000); // Create input event INPUT ip; ip.type = INPUT_KEYBOARD; ip.ki.time = 0; ip.ki.dwFlags = KEYEVENTF_UNICODE; ip.ki.wScan = 'd'; ip.ki.wVk = 0; ip.ki.dwExtraInfo = 0; SendInput(1, &ip, sizeof(INPUT)); // Exit normally return 0; }Thanks! that worked wonderfully!
Great, best of luck with the rest of your program!
Hey, thanks for the piece of code. It’s just what I was looking for (hate all that C++ and C# things).
It’s pretty easy to understand.
I played with it a bit and I can send a ‘CTRL+V’ command.
Thanks again for the code !
Thanks Pyroesp, I’m glad you found it useful!
can u please tell me how u send ‘CTRL+V’ as sendInput..
I’ve just posted an example of this:
http://batchloaf.wordpress.com/2012/10/18/simulating-a-ctrl-v-keystroke-in-win32-c-or-c-using-sendinput/
Wow, exactly what I needed and nice easy to read code. A+ from a fellow C++ programmer!
Thanks Cody!
Ted
Pingback: Boot Straight to Desktop in Windows 8 | Black-Pixel
Can anybody please tell me how to send a key to a window which is not in focus?
Thanks in advance
I’ve just posted an example showing how to do this. It’s a little bit messy, but it works. Here’s the link:
Sending a key to a winndow that’s not in focus (C program)
Thanks you for the code. It’s just what I was looking for.
You’re welcome!