Console ReadKey async or callback?

前端 未结 6 882
萌比男神i
萌比男神i 2021-01-04 14:04

I am trying to do a press Q to quit thing in the console window. I dont like my current implementation. Is there a way i can async or use a callback to get keys from the con

6条回答
  •  误落风尘
    2021-01-04 14:28

    Here is an implementation that I created using KeyAvailable. This keeps a prompt at the bottom of the console window while everything "printed" to the console starts from the top.

    public class Program
    {
        private static int consoleLine;
        private static int consolePromptLine;
        private static bool exit;
        static string clearLine = new string(' ', Console.BufferWidth - 1);
    
        public static void Main(string[] args)
        {
            StringBuilder commandCapture = new StringBuilder(10);
            string promptArea = "Command> ";
    
            consolePromptLine = Console.WindowTop + Console.WindowHeight - 1;
    
            ClearLine(consolePromptLine);
            Console.Write(promptArea);
    
            while (!exit)
            {
                // Do other stuff
    
                // Process input
                if (Console.KeyAvailable)
                {
                    var character = Console.ReadKey(true);
    
                    if (character.Key == ConsoleKey.Enter)
                    {
                        if (commandCapture.Length != 0)
                        {
                            ProcessCommand(commandCapture.ToString());
                            commandCapture.Clear();
                            ClearLine(consolePromptLine);
                            Console.Write(promptArea);
                        }
                    }
                    else
                    {
                        if (character.Key == ConsoleKey.Backspace)
                        {
                            if (commandCapture.Length != 0)
                            {
                                commandCapture.Remove(commandCapture.Length - 1, 1);
                                ClearLine(consolePromptLine);
                                Console.Write(promptArea);
                                Console.Write(commandCapture.ToString());
                            }
                        }
                        else
                        {
                            commandCapture.Append(character.KeyChar);
                            Console.SetCursorPosition(0, consolePromptLine);
                            Console.Write(promptArea);
                            Console.Write(commandCapture.ToString());
                        }
                    }
                }
            }
    
        }
    
        private static void ProcessCommand(string command)
        {
            if (command == "start")
            {
                Task testTask = new Task(() => { System.Threading.Thread.Sleep(4000); return "Test Complete"; });
    
                testTask.ContinueWith((t) => { Print(t.Result); }, TaskContinuationOptions.ExecuteSynchronously);
                testTask.Start();
            }
            else if (command == "quit")
            {
                exit = true;
            }
    
            Print(command);
            consolePromptLine = Console.WindowTop + Console.WindowHeight - 1;
        }
    
        public static void Print(string text)
        {
            ClearLine(consoleLine);
            Console.WriteLine(text);
            consoleLine = Console.CursorTop;
        }
    
        public static void ClearLine(int line)
        {
            Console.SetCursorPosition(0, line);
            Console.Write(clearLine);
            Console.SetCursorPosition(0, line);
        }
    }
    

提交回复
热议问题