如何在控制台中获取不同按键的按键事件

How to get keypress event in console for different keys

我尝试用谷歌搜索这个问题,但找不到答案(希望这不仅仅是我糟糕的搜索技巧)。但我目前拥有的是:

static void Main(string[] args)
    {

        while (xa == true)
        {

                switch (switchNum)
                {
                    case 0:
                        Console.WriteLine("Text");
                        break;
                    case 1:
                        Console.WriteLine("Stuff");
                        break;
                    case 2:
                        Console.WriteLine("More Stuff");
                        break;
                }       

        }

而我想知道的是当我按下一个键时如何改变switchNum。希望每个案例有 3 个不同的键。

编辑:澄清

所以目前它会像这样从控制台中输入:

Text
Text
Text
Text
Text
Text

我希望它在我按下某个键时更改为其他内容,例如"w"

Text
Text
Text
Text
//Press w here
Stuff
Stuff
Stuff
Stuff
//Pressing another key e.g. q
More Stuff
More Stuff
More Stuff
...

所以我不知道如何接收不同的密钥,以及如何保持循环(因此,readkey() 可能不起作用)。

提前致谢!

您需要使用 Console.ReadKey() 来查看何时按下某个键,并且由于该函数是一个阻塞函数,您可以使用 Console.KeyAvailable 仅在知道用户按下时调用它压东西。 您可以阅读更多 here and here.

您的代码可以更改为:

//console input is read as a char, can be used in place of or converted to old switchNum you used
char input = 'a';//define default so the switch case can use it

while (xa == true)
{
    //If key is pressed read what it is.
    //Use a while loop to clear extra key presses that may have queued up and only keep the last read
    while (Console.KeyAvailable) input = Console.ReadKey(true).KeyChar;

    //the key read has been converted to a char matching that key
    switch (input)
    {
        case 'a':
            Console.WriteLine("Text");
            break;
        case 's':
            Console.WriteLine("Stuff");
            break;
        case 'd':
            Console.WriteLine("More Stuff");
            break;
    }
}

.

编辑:正如 Jane 评论的那样,您使用的循环没有限制器,它将以 CPU 运行 的速度循环。您绝对应该在循环中包含 System.Threading.Thread.Sleep() 以在每个循环之间休息,或者通过将 while (Console.KeyAvailable) key = Console.ReadKey(true).KeyChar 转换为 key = Console.ReadKey(true).KeyChar

在打印之前等待一个键

我正在研究基于每次短时间间隔后超时 ReadKey() 的解决方案 Refer 用 Console.Write("\b");在输入的键上退格。

但是 @Skean 的 解决方案很棒。基于他的回答:-

static void Main(string[] args)
    {
        Console.WriteLine("start");
        bool xa = true;
        int switchNum = 48; // ASCII for '0' Key
        int switchNumBkup = switchNum; // to restore switchNum on ivalid key
        while (xa == true)
        {    
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                switchNumBkup = switchNum; // to restore switchNum on ivalid key
                switchNum = key.KeyChar; // Get the ASCII of keyPressed
                if (key.Key == ConsoleKey.Escape)
                {
                    Console.WriteLine("Esc Key was pressed. Exiting now....");
                    Console.ReadKey(); // TO Pause before exiting
                    break;
                }
            }
            switch (switchNum)
            {

                    case 48: //ASCII for 0
                        Console.WriteLine("Text");
                        break;
                    case 49: //ASCII for 1
                        Console.WriteLine("Stuff");
                        break;
                    case 50: //ASCII for 2
                        Console.WriteLine("More Stuff");
                        break;
                    default :
                        Console.WriteLine("Invalid Key. Press Esc to exit.");
                        switchNum = switchNumBkup;
                        break;
            }                
            System.Threading.Thread.Sleep(200) ;                
        }
        Console.WriteLine("Exiting ");
    }