Posted on March 18th, 2008
7190 views Simulating Keys Simplified 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( 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 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 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> 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 |