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

时间:2015-01-06 01:11:23

标签: c# keypress

我尝试使用谷歌搜索,但我找不到答案(希望这不仅仅是我糟糕的搜索技巧)。但我现在拥有的是:

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()可能不起作用。)

提前致谢!

2 个答案:

答案 0 :(得分:3)

您需要使用Console.ReadKey()来查看按键的时间,并且由于该功能是阻止功能,您可以使用Console.KeyAvailable仅在您知道用户按某事时才调用它。 您可以阅读更多herehere

您的代码可以更改为:

//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

答案 1 :(得分:1)

我在每个短间隔Refer之后开始基于超时ReadKey()的解决方案 使用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 ");
    }