• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[C] Simulating Keys Simplified!

Status
Not open for further replies.
Level 6
Joined
Nov 1, 2007
Messages
42
Ok, for those who don't know already, the simplest way to simulate a key press is by using the Windows API function:

Code:
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:

Code:
VOID keybd_event(      
    BYTE [I]bVk[/I],
    BYTE [I]bScan[/I],
    DWORD [I]dwFlags[/I],
    PTR [I]dwExtraInfo[/I]
);

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:

Code:
#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"

Code:
// 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:

Code:
#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:

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

int main()
{
    sleep(5000);

    keybd_event(H, 0, 0, 0); 
    keybd_event(I, 0, 0, 0); 
    keybd_event(V, 0, 0, 0); 
    keybd_event(E, 0, 0, 0); 
    keybd_event(W, 0, 0, 0);
    keybd_event(O, 0, 0, 0);
    keybd_event(R, 0, 0, 0);
    keybd_event(K, 0, 0, 0);
    keybd_event(S, 0, 0, 0);
    keybd_event(H, 0, 0, 0);
    keybd_event(O 0, 0, 0);
    keybd_event(P, 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 "hiveworkshop.com".

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

Regards, GoSu.SpeeD
 
Status
Not open for further replies.
Top