문제

I am currently executing a GetAsyncKeyState event handler when the 'c' key is pressed down in C++.

Here is my code:

     bool isKeyPressed = false;
 void someFuntionOne()
 {

     if( GetAsyncKeyState( 'C' ) & 0x8000)
    {

       if(isKeyPressed)
       {
           isKeyPressed = false;
       }
       else
       {
           isKeyPressed = true;
       }    
    }   
}

void someFunctionTwo()
{
   if(isKeyPressed)
   {
    // Do something     
   }
}

So bassically I want to know if the 'C' has been pressed not held down so I use my boolean variable isKeyPressed to tell me if the key was pushed down at any point. If it was pressed, set isKeyPressed to true and if it was pressed again set isKeyPressed to false.

The problem I have with this is I am running a huge OpenGL/C++ program that takes a lot of time to go around and execute the entire program in a single iteration so if the user presses down on the 'C' key, the program may not catch that event in time because it is still executing the rest of the other code and is currently not at the section of the code where it checks if GetAsyncKeyState( 'C' ) & 0x8000.

Is there a better way I can check for this event where the program is always listening for the event to happen at any point of execution time? How can I implement this?

올바른 솔루션이 없습니다

다른 팁

Usually whoever delivers the events to your application will queue them until the application has the chance to process them, i.e. at the end of each main loop iteration. So ideally you would react to the events at that stage. An (ugly) alternative is something like:

bool keyWasPressed = false;
void processKeyEvent() {
     if( <C is pressed>) {
         keyWasPressed = true;
     }
}
void someFunction() {
    if(keyWasPressed) {
        keyWayPressed = false;
        // Do something     
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top