Flush GetAsyncKeyState buffer???

Ok, so I'm working on the final project for my Computer Science 2 class and I'm writing a Who Wants To Be A Millionaire game for it. I'm using GetAsyncKeyState in a time controlled while loop for the questions so that I could implement a timer. Problem is, after I press a key and call another menu, it automatically puts the previous key(s) in. Is there any way to clear the keyboard buffer for GetAsyncKeyState?

I found somewhere that said to use this:
1
2
3
4
if (cin.peek() != EOF)
{
     cin.ignore(numeric_limits<streamsize>::max());
}

The program won't run with that so I found that I could replace numeric_limits<streamsize>::max() with LONG_MAX.
However that causes the program to pause indefinitely. I need something that won't cause the program to pause at all.

Here's the loop I'm currently using for the timer:
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
27
28
29
30
bool keyDown(int vKey)
{
     return((GetAsyncKeyState(vKey) & 0x8000) ? 1 : 0);
}

start=clock();
while(dif<30 && run==true)
{
     for(int x=65x<91;x++)  //checks x against the ascii values for the alphabet
     {
          if(keyDown(x))
          {
               answer=static_cast<char>(x);
               checkAns(quesNum);    //checks answer entered
               quesNum++;            //increments the question
               dispQues(quesNum);    //displays the next question
          }
     }

     end=clock();
     dif=diftime(end,start)/CLOCKS_PER_SEC;
     dif=floor(dif);
     tim=30-dif;    //tim is the number of seconds remaining that is displayed

     placeCursor(screen,20,48);
     cout << "  ";
     placeCursor(screen,20,48);
     cout << tim;
     Sleep(25)
}


So, any ideas on something to clear the buffer without pausing anything?

Thanks for any help.
Last edited on
GetAsyncKeyState doesn't work with a buffer. That's what the "Async" (asynchronous) part of its name means. It just checks if the key is pressed when you call it. The problem arises simply because you don't give the user enough time to take his finger from the key... Try sleeping for more time, Sleep(1000); for example, this would do the job, I suppose.
Doesn't work. When a key is pressed that tells it to go to another menu, it then enters that previous key for the next menu as well. Or, since here it will only execute if you enter a letter, if you press a bunch of numbers and/or symbols nothing happens, but then if you press a letter it outputs all the numbers and/or symbols that you pressed before to the screen as input. It's somehow logging all keystrokes and not clearing them out.
mmmm... would you mind showing me some more code? what do checkAns and dispQues do?
Nevermind, I took another look through my code, line by line, and found that in my checkAns function when it goes to check if the entered answer was correct I still had several spots that were using cin, and that appears to solve the issue. Apparently I can't use cin anywhere after I use GetAsyncKeyState, so I'm in the process of converting all input portions of the program to use GetAsyncKeyState. Also, where I was having the problem of the input not working, it was because I wasn't treating the number option entered as an ascii value, rather I was treating it as a plain old int.

Thanks anyway though! I'll be back if I have anymore issues :P
Last edited on
Topic archived. No new replies allowed.