Publishing System Settings Logout Login Register
[C] Simulating Keys Simplified!
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on March 18th, 2008
6070 views
C++ General
Simulating Keys Simplified

Pixel2Life.com
Shodoukoutei.co.uk


Ok, for those who don't know already, the simplest way to simulate a key press is by using the Windows API function:

keybd_event();


The function "keybd_event" takes 4 parameters, by looking at msdn (http://msdn2.microsoft.com/en-us/library/ms646304(VS.85).aspx) we can see what these parameters are:

VOID keybd_event(     
    BYTE bVk,
    BYTE bScan,
    DWORD dwFlags,
    PTR dwExtraInfo
);


to simulate a key press we only we need to supply the function with the first parameter, which is a Virtual Key Code (will be referred to as VKC from now on).  Here is a link to the full list of VKC's: http://msdn2.microsoft.com/en-us/library/ms645540(VS.85).aspx

Ok, so with this information we are able to simulate any key presses with "keybd_event" like so:

#include <windows.h> // needed for keybd_event

int main()
{
    sleep(5000); /* Allow user 5 seconds to switch to an application
                          such as notepad, to view the results of the keypress */
    keybd_event(0x54, 0, 0, 0); // Simulate the pressing of the key 't'
    return 0;
}


Right, so the above code when compiled and executed will wait 5 seconds for the user to switch to notepad (or anything which allows text input) then the program will simulate the pressing of the key 't' and 't' should appear in notepad.



Well, a while ago, i got annoyed of having to CONSTANTLY refer to the msdn page every time i wished to find out the VKC for a specific key, so i created this Header File, which basically gives a different name for the key codes that i used the most, so that i could remember them easily.

So all you have to do is copy the following code into a blank source file and save it as "keybd.h"

// Simulating Keys Simplified by GoSu.SpeeD

#define BACKSPACE 0x08
#define TAB 0x09
#define CLEAR 0x0C
#define ENTER 0x0D
#define SHIFT 0x10
#define CTRL 0x11
#define MENU 0x12
#define PAUSE 0x13
#define CAPS 0x14
#define ESC 0x1B
#define SPACE 0x20
#define ZERO 0x30
#define ONE 0x31
#define TWO 0x32
#define THREE 0x33
#define FOUR 0x34
#define FIVE 0x35
#define SIX 0x36
#define SEVEN 0x37
#define EIGHT 0x38
#define NINE 0x39
#define A 0x41
#define B 0x42
#define C 0x43
#define D 0x44
#define E 0x45
#define F 0x46
#define G 0x47
#define H 0x48
#define I 0x49
#define J 0x4A
#define K 0x4B
#define L 0x4C
#define M 0x4D
#define N 0x4E
#define O 0x4F
#define P 0x50
#define Q 0x51
#define R 0x52
#define S 0x53
#define T 0x54
#define U 0x55
#define V 0x56
#define W 0x57
#define X 0x58
#define Y 0x59
#define Z 0x5A
#define DOT 0xBE


Now, you might be thinking, "Ok so, how do i access this from my program?".
well the answer is simple, just copy the header file (keybd.h) into the same directory as the source code of the program you wish to use it with, and add this line to your source code of your main program:

#include "keybd.h"


now that, that's done we can use the keybd_event function to produce strings much faster than we could've because of not having to refer to the VKC msdn page:

#include <windows.h>
#include "keybd.h"

int main()
{
    sleep(5000);

    keybd_event(P, 0, 0, 0);
    keybd_event(I, 0, 0, 0);
    keybd_event(X, 0, 0, 0);
    keybd_event(E, 0, 0, 0);
    keybd_event(L, 0, 0, 0);
    keybd_event(TWO, 0, 0, 0);
    keybd_event(L, 0, 0, 0);
    keybd_event(I, 0, 0, 0);
    keybd_event(F, 0, 0, 0);
    keybd_event(E, 0, 0, 0);
    keybd_event(DOT, 0, 0, 0);
    keybd_event(C, 0, 0, 0);
    keybd_event(O, 0, 0, 0);
    keybd_event(M, 0, 0, 0);

    return 0;
}


what this code will do is, wait 5 seconds for the user to open notepad (or any other program which will accept text as input), then the program will type "pixel2life.com".

So there you go, simulating keys simplified (in my opinion).

Regards, Shoxin
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Encrypt

Hello, my name is Daniel. I live in the UK (Northern Ireland) and have done for my whole life. On the computer i enjoy programming, reverse engineering and playing games every now and again.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top