Problem Regarding GetAsyncKeyState()

Hi all ,
I am working on a keylogger application. I have used the function GetAsyncKeyState
method to get the key pressed.

while(1)
{
if(GetAsyncKeyState(0x41)) // 0x41 is the virtual key code for A/a.
{
cout<<"A";
}
}

Actually i have successfully recorded every key. but now the problem i am facing is that when i type slowly every key is recorded successfully. but as i type fast the keys are either no captured or are replaced each other e.g if i type nauman
it will write it as nuamna or something else.


Please help me out
Are you using a keydown/keyup checker?

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
bool keydown=false;
while (1)
{
     if (GetAsyncKeyState(0x41)) 
     {
          if (!keydown)
          {
              cout<<"A";
              keydown=true;
          }
     }
     else if (GetAsyncKeyState(0x42))
     {
         if (!keydown)
         {
             cout<<"B";
             keydown=true;
         }
     }
//etc... etc...
     else
     {
          //if no key is pressed
          keydown=false;
     }
}


EDIT: added proper spacing and fixed typo.
Last edited on
Sargon , Thanks for replying and helping me.

Actually i want to make a keylogger application that captures the key entered by the user of the system and store it in a file.

I am able to get all the keys with GetAsyncKeyState even without the use of bool . The problem i am facing actually is when i type fastly it is not able to correctly interpret the key pressed(or someother issue i dont know.)

when i type slowly it works very well .

So , there is nothing with key capturing i have captured them all but it is not able to store them correclty when user types fast.

Hope you understood . If not yet we can have a call via skype(ofcourse, if you allow)
Ok, odd. Usually if you try to make a keylogger (without the bool trick I showed you) the problem comes from getting too many keys with one stroke. (Inputting hello would output hhheeelllllllloooo or something similar). Are you using some time of sleep function in your while loop? Or something that could seriously slow down performance?
sargon, thanks again

And yes i am using Sleep() method. to control the input from being repeated.
But why it slows too much to mis interpret (either replaced or missed)the keys pressed?

Sory for late responding as i am having internet connectivity issues from me ISP.
When you use Sleep (), you're essentially telling your program to shutdown while it's sleeping. So sleep(1000)->shutdown for a second. During the sleep time it doesn't do anything including grabbing keys from the keyboard.

If you want to control the input from being repeated, use the method I wrote above (it's far more accurate). I'd recommend either getting rid of sleep entirely or turning it way down to something like Sleep(1). Sleep(1) will keep your program from flooring your processor while also allowing all human inputs through (no one types for less than a millisecond).
Topic archived. No new replies allowed.