Turn off input in C # console until a specific task is completed

I am working on a small part of my program, handling input, basically I have this little code:

bool Done = false;
while (!Done)
{
  ConsoleKeyInfo key = Console.ReadKey(true);
  if (key.Key == ConsoleKey.Enter)
  {
    //Action
  }
}

The main problem is that the code will handle ReadKey even between actions.

So, if you have a menu in which you can press keys, and then it will say “you pressed: x”, if you press any buttons while it shows you this message, it ReadKeyalready receives this new key.

So, I want to block any additional input until the user sees the menu again.

+3
source share
2 answers

, , , , . :

while (!Done)
{
    while (Console.KeyAvailable) Console.ReadKey(true);
    ConsoleKeyInfo key = Console.ReadKey(true);
    // etc..
}
+5

, , .

.

0

Source: https://habr.com/ru/post/1795324/


All Articles