C# 按键是否排队等待分配?

C# 按键是否排队等待分配?,c#,keyboard,console-application,keyboard-events,C#,Keyboard,Console Application,Keyboard Events,我不知道该怎么回答这个问题 在我的应用程序中,如果用户在ConsoleKeyInfo对象被分配到Console.ReadKey()之前按enter键,它会自动将其设置为用户按的任何键。事实上,它似乎是堆叠的,因为如果用户粉碎enter 5次,它将自动分配5次,这对我来说非常糟糕。在调试过程中,我非常仔细地遵循了这一点,我很有信心这就是所发生的事情。使用Thread.Sleep()创建用户可以多次按enter键的机会窗口。有人警告我不要使用它,但我认为它对我来说很好,因为我需要所有的东西都停一下,

我不知道该怎么回答这个问题


在我的应用程序中,如果用户在ConsoleKeyInfo对象被分配到Console.ReadKey()之前按enter键,它会自动将其设置为用户按的任何键。事实上,它似乎是堆叠的,因为如果用户粉碎enter 5次,它将自动分配5次,这对我来说非常糟糕。在调试过程中,我非常仔细地遵循了这一点,我很有信心这就是所发生的事情。使用Thread.Sleep()创建用户可以多次按enter键的机会窗口。有人警告我不要使用它,但我认为它对我来说很好,因为我需要所有的东西都停一下,以便用户可以阅读一行文本。我在想也许这样的机会永远不会存在?或者我对这里发生的事情的解释不正确?我只要求在整个应用程序中输入一行代码…

如果我正确地阅读了您所说的内容,您会不喜欢这样的方式,即按键时所有按键都排成队列,然后只有在您执行
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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
名称空间控制台App6
{
班级计划
{
公共静态void Main()
{
ConsoleKeyInfo ckilast=默认值(ConsoleKeyInfo);
ConsoleKeyInfo CKICURENT=默认值(ConsoleKeyInfo);
Console.WriteLine(“\n按一个键显示;按“x”键退出”);
//调整这个“窗口”周期,直到感觉合适为止
//TimeSpan tsignorerepeatedkeypresstimeperiod=新的TimeSpan(0,0,0,0,0);//0表示无额外延迟
TimeSpan tsignorerepeatedkeypresstimeperiod=新的时间跨度(0,0,0,0,250);
做
{
while(Console.KeyAvailable==false)
Thread.Sleep(250);//循环直到输入。
ckilast=默认值(ConsoleKeyInfo);
DateTime eatingendtime=DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod);//将忽略重复窗口中相同键的任何“重复”按键
做
{
while(Console.KeyAvailable==true)
{
ckiccurrent=Console.ReadKey(true);
if(ckicCurrent.Key==ConsoleKey.X)
打破
如果(ckiccurrent!=ckilast)//上次按了不同的键,那么让它得到处理
{
eatingendtime=DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod);//重置窗口时段
WriteLine(“您按下了“{0}”键。”,ckiccurrent.key);
ckilast=CKI电流;
继续;
}
}
if(Console.KeyAvailable==false)
{
睡眠(50);
}
}while(DateTime.UtcNow