简体   繁体   English

C# .NET 控制台应用程序获取键盘输入

[英]C# .NET Console Application getting keyboard input

I am trying to make a console application and it (don't ask me why) requires keyboard input (DownArrow and UpArrow).我正在尝试制作一个控制台应用程序,它(不要问我为什么)需要键盘输入(DownArrow 和 UpArrow)。 So far so good.到现在为止还挺好。 The keyboard input needs to be in a While(true) loop and run forever, it is checked with an if statement.键盘输入需要在 While(true) 循环中并永远运行,它使用 if 语句进行检查。 I tried using Console.ReadKey() == ConsoleKey.UpArrow;我尝试使用Console.ReadKey() == ConsoleKey.UpArrow; but that seems to pause the loop from running.但这似乎会暂停循环运行。 Anyway I can run an if(Keyboard.input == Key.UpArrow){} statement without pausing the loop (basically skipping over it if it isn't the case)?无论如何,我可以在不暂停循环的情况下运行if(Keyboard.input == Key.UpArrow){}语句(如果不是这种情况,基本上跳过它)?

Here is an example of what I mean:这是我的意思的一个例子:

while (true){

if (Console.ReadKey() == ConsoleKey.UpArrow){ // this pauses the loop until input, which is not what I want / need.
 // Do stuff
}
Frame.Update();
}

The trick is that you first need to check if there is a KeyAvailable in the cache, and then use ReadKey() to read it.诀窍在于,首先需要检查缓存中是否存在KeyAvailable然后使用ReadKey()读取它。 After that, your code should work as you expect.之后,您的代码应该可以按预期工作。 The line of code that does this would look something like:执行此操作的代码行类似于:

// Check if there's a key available. If not, just continue to wait.
if (Console.KeyAvailable) { var key = Console.ReadKey(); }

Here's a sample to demonstrate:这是一个示例来演示:

public static void Main(string[] args)
{
    Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
    Console.CursorVisible = false;
    Console.Write('*');

    var random = new Random();

    while (true)
    {
        if (Console.KeyAvailable)
        {
            var key = Console.ReadKey(true);

            switch (key.Key)
            {
                case ConsoleKey.UpArrow:
                    if (Console.CursorTop > 0)
                    {
                        Console.SetCursorPosition(Console.CursorLeft - 1, 
                            Console.CursorTop - 1);
                        Console.Write('*');
                    }
                    break;
                case ConsoleKey.DownArrow:
                    if (Console.CursorTop < Console.BufferHeight)
                    {
                        Console.SetCursorPosition(Console.CursorLeft - 1, 
                            Console.CursorTop + 1);
                        Console.Write('*');
                    }
                    break;
                case ConsoleKey.LeftArrow:
                    if (Console.CursorLeft > 1)
                    {
                        Console.SetCursorPosition(Console.CursorLeft - 2, 
                            Console.CursorTop);
                        Console.Write('*');
                    }
                    break;
                case ConsoleKey.RightArrow:
                    if (Console.CursorLeft < Console.WindowWidth - 1)
                    {
                        Console.Write('*');
                    }
                    break;
            }
        }

        // This method should be called on every iteration, 
        // and the iterations should not wait for a key to be pressed
        // Instead of Frame.Update(), change the foreground color every three seconds  
        if (DateTime.Now.Second % 3 == 0) 
            Console.ForegroundColor = (ConsoleColor) random.Next(0, 16);
    }
}

Console.ReadKey locks thread. Console.ReadKey锁定线程。 You should create another thread and read key there.您应该创建另一个线程并在那里读取密钥。 Example Here:示例在这里:

Task.Factory.StartNew(() =>
            {
                if (Console.ReadKey().Key == ConsoleKey.UpArrow)
                {
                    Console.WriteLine("Pressed");
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM