禁用C#控制台中的输入,直到完成某些任务 [英] Disabling input in C# Console until certain task is completed

查看:104
本文介绍了禁用C#控制台中的输入,直到完成某些任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理程序的一小部分,处理输入,基本上我有这个小代码:

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

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

主要问题是代码即使在两次操作之间也将处理ReadKey。

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

因此,如果您有一个可以按键的菜单,然后在显示此消息时按任何按钮,则会显示您按了:x, ReadKey 已获取该新密钥。

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..
}

这篇关于禁用C#控制台中的输入,直到完成某些任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆