?
Solved

GetAsyncKeyState consuming almost all CPU

Posted on 2007-03-20
5
Medium Priority
?
396 Views
Last Modified: 2013-12-04
Hi all,

I am currently using GetAsyncKeyState to launch an application. It works when the user presses a combination of keys, then creates the target application using CreateProcess. However, it seems that my PC is beginning to be sluggish since I had that app running. It loads from start up via the registry key

HKLM/Software/Microsoft/Current Version/Run (can't remember if the path is right, but you get what I mean)

The program is windowless and runs in a loop checking for the correct key combinations entered by the user.

A snippet of the code is as follows.

while(1)
{
....................................
if(GetAsyncKeyState(VK_CONTROL))             // Ctrl + Alt + G
                if(GetAsyncKeyState(VK_MENU))
                        if(GetAsyncKeyState('G'))
                                     launchApp();
....................................
}

In launchApp

launchApp()
{
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

        CreateProcess( NULL, "pxapp.exe", NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
}

I guess is that loop while(1) that is consuming all the resources, so it checks for key every few miliseconds, not elegant. Is there any way of optimising this program? Perhaps put it in idle state and trigger an event when received those key combinations. Will it help if I include a Sleep in the loop? Any ideas?

Thanx.
0
Comment
Question by:fart_boy21
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 800 total points
ID: 18762943
Simply put a sleep ( 10 ) or similar to release the CPU:


while ( 1 )
{
   if(GetAsyncKeyState(VK_CONTROL))             // Ctrl + Alt + G
                if(GetAsyncKeyState(VK_MENU))
                        if(GetAsyncKeyState('G'))
                                     launchApp();
   Sleep ( 10 );  
}

HTH
0
 

Author Comment

by:fart_boy21
ID: 18767454
Oh. Thats what I thought. But then will it not miss some keystrokes since the app is "sleeping"?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18769359
If your Sleep() interval is too long then, yes, you may miss a keystroke here and there.  You could probably go up to 50 though without losing anything...

An alternative would be to use a low level keyboard hook via WH_KEYBOARD_LL.
0
 

Author Comment

by:fart_boy21
ID: 18784447
Ok thanks all for the answers. The Sleep really helped. I am just a beginner in Windows programming. I do not know the architechure of it.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 18806748
Glad you got it sorted.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question