I wrote a C++ console program that displays a menu with several options for the user, one of them goes into a continuous loop and the condition to exit the loop is !GetAsyncKeyState(VK_ESCAPE). So if a user presses ESC, on the next start of this loop it will exit and the main menu will be displayed again.
The problem happens when the user doesn't have my program on focus and presses the ESC key, the GetAsyncKeyState will still return that the ESC key was pressed and the loop will exit.
How can I implement a solution that will listen to the ESC key only when my program is on focus?
- I can't use getline, getch or any line input methods because I will have the loop running and I can't ask for user input on every iteration.
- The solution can be platform dependent since this will only be running on Windows.
- I have to stick with C++ because I am using a library that the only "usable" implementation is the C++ version.
This could be useful to anyone else that would write a c++ console program that has a loop that exits when a key in pressed and the program is on focus.
Thanks in advance!