I'm writing a sample console application in VS2008. Now I have a Console.WriteLine()
method which displays output on the screen and then there is Console.ReadKey()
which waits for the user to end the application.
If I press Enter while the Console.WriteLine()
method is displaying then the application exits.
How can I clear the input buffer before the Console.ReadKey()
method so that no matter how many times the user presses the Enter button while the data is being displayed, the Console.ReadKey()
method should stop the application from exiting?
Clear command is not the only way to clear the terminal screen. You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators. If you use Ctrl+L and clear command in GNOME terminal (default in Ubuntu), you'll notice the difference between their impact.
The console. clear() method clears the console if the console allows it. A graphical console, like those running on browsers, will allow it; a console displaying on the terminal, like the one running on Node, will not support it, and will have no effect (and no error).
This method clears the console and displays console was cleared message. Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console. On MacOS you can use Command + K button.
Just put system("cls"); line before you start printing anything.
Unfortunately, there is no built-in method in Console class. But you can do this:
while(Console.KeyAvailable)
Console.ReadKey(false); // skips previous input chars
Console.ReadKey(); // reads a char
Use Console.ReadKey(true)
if you don't want to print skipped chars.
Microsoft References
Console.KeyAvailable
Console.ReadKey(bool)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With