Disabling console input

I have a program that uses std::cin to determine which function to call based on the input.

Some of the functions in the program use iostream for input/output and at the end of the function I use getch(), pressing 'm' (for more) will return -1 and leave the console window open for more commands to be entered. Any other key will hide the console window and the program will wait for the specified key combination to reveal the console window again.

A new function I just finished coding needs to execute code in an infinite while loop and since getch() will halt execution of the program until a key is pressed, I just used:

if(GetAsyncKeyState('M') & 0x8000)
return -1;

The problem with this approach, I found, is that when the function returns to await input, there's an 'm' in the console window. I've tried a bunch of cin's functions (ignore() for example), the '\b' char (which I immediately realized was dumb and wouldn't work lol).. even the BlockInput() function.

Any keys that I press during the execution of the function will show up in the console window after the function returns. I was about to resort to using a backspace keyboard event every time I pressed a key, then I decided to just ask people who know more about the standard input stream buffer, etc.

Thanks in advance if anyone knows how to solve my problem.
The standard library does not allow a way to do this, as it is only designed for basic I/O.

Since you're already using WinAPI for GetAsyncKeyState, you can use it to flush the input buffer as well:

 
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));


That line will flush the input buffer, which effectively discards all pending input data that the user input but that your program did not poll for yet.


Only thing to know is that anything from WinAPI (Windows.h) will make your program Windows-only (obviously).
Last edited on
Worked like a charm. Thanks :D
Topic archived. No new replies allowed.