I have a simple timer. It's in a function running in a thread separate from the main. Using std::future, the function returns a simple bool that says whether the timer has hit a specific number or not.
I am using getch(); to see if the user pressed a letter key.
If the timer returns true, that it hit a designated number, I need to cancel getch(); and move to the next step in the code. Moving to the next step is easy.
It's been 2 weeks and I can not find a solution to my problem.
The Problem: How on earth, can I interrupt or cancel a call to getch();? Is this even possible?
I'm using getch(); to identify which letter keys were pressed.
C++11 Visual Studio.
This code will allow you to do what you want, but it does not take advantage of newer language features, nor is it portable.
You are not going to be able to break out of
getch(). The best bet is to check for data in the STDIN buffer and only make the call after you have something to read. This example useskbhit(), but instead of using a polling loop where it periodically checks for buffer activity, it hooks the underlying handle to the input stream and waits for activity.Using a second thread as a one-shot timer is also not the most efficient way to go. The timer in this code uses a Microsoft specific object. It is coded to fire off every ten seconds, but you can certainly change that.