Hello,

I am using a computer vision C++ library called OpenCV, which has a function "int waitKey(int delay)". This function waits for a user input key, for a specified number of milliseconds. It returns if either a key is pressed, or the time elapsed is equal to the value of "delay".

I have two questions.

1. Is there a way of doing this in the standard C++ library?

2. It is often necessary to hold down the keyboard key in order for waitKey() to recognize the input. This is because waitKey() is in a loop that also contains other functions that take up time (see below), and so the key needs to be held down if it is to fall within waitKey()'s timing window. The problem with this is that next time I call waitKey(), it returns the value of the key that was held down in the previous loop - rather than the value of the key that has been pressed in the current loop. As such, after the first loop, waitKey() always returns without actually pressing a key.

So, it seems like there is some sort of buffer that does not get cleared. I have tried called cin.ignore(1000, '\n') as I have been recommended before, but this does not solve the problem. Perhaps this is a specific problem related to OpenCV?

Thanks for the help.

Code:
while (true)
{
    char key = waitKey(100);
    /* Perform some other functions - this requires the keyboard key to be held down if it is to be recognized by waitKey()'s 100 millisecond window
     ........
     ........
     */
}