Is there any alternative for GetAsyncKeyState( int ) function in visual c++?
-
I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:
while( 1 )
{
for( int i = 0; i < 256; i++ )
{
GetAsyncKeyState( i );
}
}But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?
-
I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:
while( 1 )
{
for( int i = 0; i < 256; i++ )
{
GetAsyncKeyState( i );
}
}But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?
You should use a global keyboard hook (see: "Using Hooks") for the purpose. Are you writing a keylogger? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You should use a global keyboard hook (see: "Using Hooks") for the purpose. Are you writing a keylogger? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thanks for your reply Pallini. I was curious about how keylogger works and while searching, i got a keylogger code in c++. Now i am just trying to edit it, just for a learning purpose. :|
-
Thanks for your reply Pallini. I was curious about how keylogger works and while searching, i got a keylogger code in c++. Now i am just trying to edit it, just for a learning purpose. :|
Here's some neat theory on hooks and DLLs and a practical example to illustrate it all, by Dr. Joseph Newcomer: Hooks and DLLs[^]
“Follow your bliss.” – Joseph Campbell
-
I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:
while( 1 )
{
for( int i = 0; i < 256; i++ )
{
GetAsyncKeyState( i );
}
}But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?
I see you already found a better way to do what you want, so this is just an "additional footnote" to your original problem: to make your while loop less CPU hungry you can add a Sleep(1) call somewhere inside it, or maybe use SwithToThread[^] (althorough SwitchTothread seems to be much less effective than Sleep), this way the loop yields execution to other threads and does not eat up all the CPU power. I mean something like:
while( 1 )
{
for( int i = 0; i < 256; i++ )
{
GetAsyncKeyState( i );
}
Sleep(1);
}> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <