如果我正在阅读你说的正确的话,你不喜欢按下按键时所有排队的方式,然后只有当你做Console.Readkey
....时会被删除,并且当你重复接近时会引起你的问题。
以下是尝试按照可能有所帮助的方式处理按键队列 - 根据需要对其进行调整。我不知道你是否只是使用键盘进行“选项”选择,或对于自由文本输入等,但我认为它可以提供帮助。
到这里看看:
我已经适应,如此,它会尝试检测“重复”按键,它会“忽略他们” /“吃”如果它们在一定的时间范围内一起发生......但会继续前进,如果它是不同的按键。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
public static void Main()
{
ConsoleKeyInfo ckilast = default(ConsoleKeyInfo);
ConsoleKeyInfo ckicurrent = default(ConsoleKeyInfo);
Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");
// Adjust this "window" period, till it feels right
//TimeSpan tsignorerepeatedkeypresstimeperiod = new TimeSpan(0, 0, 0, 0, 0); // 0 for no extra delay
TimeSpan tsignorerepeatedkeypresstimeperiod = new TimeSpan(0, 0, 0, 0, 250);
do
{
while (Console.KeyAvailable == false)
Thread.Sleep(250); // Loop until input is entered.
ckilast = default(ConsoleKeyInfo);
DateTime eatingendtime = DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod); // will ignore any "repeated" keypresses of the same key in the repeat window
do
{
while (Console.KeyAvailable == true)
{
ckicurrent = Console.ReadKey(true);
if (ckicurrent.Key == ConsoleKey.X)
break;
if (ckicurrent != ckilast) // different key has been pressed to last time, so let it get handled
{
eatingendtime = DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod); // reset window period
Console.WriteLine("You pressed the '{0}' key.", ckicurrent.Key);
ckilast = ckicurrent;
continue;
}
}
if (Console.KeyAvailable == false)
{
Thread.Sleep(50);
}
} while (DateTime.UtcNow < eatingendtime);
} while (ckicurrent.Key != ConsoleKey.X);
}
}
}