【问题标题】:How to get rid of the ReadKey() que if a person where to spam a key?如果有人在哪里发送垃圾邮件,如何摆脱 ReadKey() 问题?
【发布时间】:2021-10-23 12:16:32
【问题描述】:

我的控制台应用程序通过代码使用 ReadKeys;如果一个人在 ReadKey 未激活时单击某个键,我推测它会被添加到某种队列中,因此当代码到达 readKey 时,它会立即继续前一个按键并继续,就好像那个键被按下。 有没有办法解决这个问题?

编辑:澄清一下,轴向按键会影响尚未运行的 ReadKeys

【问题讨论】:

    标签: c# console readkey


    【解决方案1】:

    您可以检查输入流中是否有任何可用的键,然后读取下一个键。

    可以在这里找到一个非常好的解决方案:Clear Console Buffer

    这里有另一个类似的解决方案: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();
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2021-08-11
      相关资源
      最近更新 更多