开发者

Disabling input in C# Console until certain task is completed

I'm working on a little part of my program, handling the input, basically I have this little code:

开发者_Python百科
bool Done = false;
while (!Done)
{
  ConsoleKeyInfo key = Console.ReadKey(true);
  if (key.Key == ConsoleKey.Enter)
  {
    //Action
  }
}

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

So if you have a menu where you can press keys and then it would say "you pressed: x" if you press any buttons while it shows you this message, the ReadKey already gets that new key.

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


Not so sure this make sense, personally I like it when keystrokes don't disappear and I can type ahead. But you can flush the keyboard buffer like this:

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


You can not block input, Even if you do not process it, it goes to the keyboard buffer.

You can simply stop getting them out of the buffer though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜