ignore key presses during sleep() function

I want to ignore all key presses while sleep function is going on.

example:

void main()
{

char option,option2;
int timer = 1500;
do
{
bool finished = true;
cout << "Press an arrow key" << endl;
option = _getch();
if(option == char(224))
{
option2 = _getch();
switch(option2)
{
case 'M' : cout << "Right key was pressed" << endl;break;
case 'P' : cout << "Down key was pressed" << endl; break;
case 'K' : cout << "Left key was pressed" << endl;break;
case 'H' : cout << "Up key was pressed" << endl;break;

};
}
if(option == '\r')
{
cout << "Return key was pressed." << endl;
}
while(finished)
{

Sleep(timer);
/*I want to ignore all key presses during the loop or until Sleep() is over.*/

}



cout << endl;
}while(!(option == 'q' || option == 'Q'));



There is a function called FlushConsoleInputBuffer() that will do what you want if you call it immediately after the Sleep function. Hope this helps.
Check this out:

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
#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char c;

    while (true)
    {
        c = _getch();

        //esc to quit
        if (c == 27) break;

        cout << c << endl;

        Sleep(1500);

        //clear the buffer
        while (_kbhit()) _getch();
    }

    cout << "hit enter to quit..." << endl;
    cin.get();

    return 0;
}
Thank you!!!!!!!
Topic archived. No new replies allowed.