Solved

GetAsyncKeyState consuming almost all CPU

Posted on 2007-03-20
5
319 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 200 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 85

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now