1

My console application uses ReadKeys thruout the code; if a person where to say click a key when an ReadKey is not active, it would to my speculation be added to a que of sorts, so when the code gets to the readKey it instantly goes on the previus keypress and continues as if that key was pressed. Is there a way to fix this?

Edit: To clarify, axidental keypresses affect the ReadKeys that have not yet been run

1 Answers1

1

You can check if there is any key available in the input stream and then read the next key.

A very good solution can be found here: Clear Console Buffer

Another similar solution here: https://newbedev.com/csharp-c-console-clear-input-buffer-code-example

// while there are characters in the input stream 
while(Console.KeyAvailable) 
{
    // read them and ignore them
    Console.ReadKey(false); // true = hide input
}
// Now read properly the next available character
Console.ReadKey();
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61