-
July 13th, 2011, 09:45 AM
#1
Clearing std::cin buffer?
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
........
........
*/
}
-
July 13th, 2011, 10:09 AM
#2
Re: Clearing std::cin buffer?
The standard library does not have a way to do this, although Windows provides kbhit() and getch() in the conio.h header, and there are implementations of these functions available for Linux online if you look around. Also, any GUI framework will provide a way to handle keyboard events.
The primary usefulness of waitKey() in OpenCV is that it acts as OpenCV's message pump. So you need to call it regularly in a loop, probably with an argument of 1 so ithat it doesn't block or wait excessively; the only purpose is to pump messages and keep the HighGUI window redrawing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|