C#使用组合键终止程序,而在读取时不按Enter

时间:2018-08-01 14:51:26

标签: c# terminate modifiers console.readkey readkey

现在这是我们所处的情况:

     getinput:
     //things happen here
     string typed = Console.ReadLine();
     try
     if (typed == "com")
     {
           //things happen here
     }
     else if  (Console.ReadKey(true).Key == (ConsoleKey.F1) + (ConsoleModifiers.Alt)) 
     {
           System.Environment.Exit(0);
     }
     //other else if's happen here
     else
     {
           Console.WriteLine("\n" + typed + " isn't an option.");
           goto getInput;
     }
     }
     catch (Exception)
     {
     }
     goto getInput;

我要做的是,当我按alt + f1时,程序将自行终止,但是因为该程序会等待我输入一些内容,即使使用工作版本(不包含alt部分),它也要我键入然后按Enter键,这是我不想要的。如何处理这个问题?

2 个答案:

答案 0 :(得分:0)

        static void Main(string[] args)
        {
            Console.TreatControlCAsInput = true;
            var typed = ReadLine();
            if (typed == "com")
            {
                Console.WriteLine("com");
                //things happen here
            }
            //other else if's happen here
            else
            {
                Console.WriteLine("\n" + typed + " isn't an option.");

            }


        }
        public static string ReadLine() {
            StringBuilder sb = new StringBuilder();
            do
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if ((key.Modifiers & ConsoleModifiers.Alt) != 0)
                {
                    if (key.Key == ConsoleKey.K)
                    {
                        Console.WriteLine("killing console");
                        System.Environment.Exit(0);

                    }
                }
                else
                {
                    sb.Append(key.KeyChar);
                    if (key.KeyChar == '\n'||key.Key==ConsoleKey.Enter)
                    {
                        return sb.ToString();
                    }
                }
            } while (true);

        }

该代码将帮助您解决问题, 只是要注意,当您按char读一行时,您需要处理退格键

答案 1 :(得分:0)

首先,请考虑使用循环而不是goto,因为goto很危险。为什么?在这里看看:'Goto' is this bad?

要解决您的问题,可以将ConsoleKeyInfo类与Console.ReadKey()方法结合使用以获取有关单键按下的信息。这样,您就可以在将单个字符加到字符串之前检查是否有任何键组合。一个有效的示例如下:

namespace Stackoverflow
{
    using System;

    class Program
    {
        public static void Main(string[] args)
        {    
            ConsoleKeyInfo keyInfo = default(ConsoleKeyInfo); 
            string input = string.Empty;

            // loop while condition is true
            while (true)
            {
                // read input character-wise as long as user presses 'Enter' or 'Alt+F1'
                while (true)
                {
                    // read a single character and print it to console
                    keyInfo = Console.ReadKey(false);

                    // check for close-combination
                    if (keyInfo.Key == ConsoleKey.F1 && (keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
                    {
                        // program terminates
                        Environment.Exit(0);
                    }

                    // check for enter-press
                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        // break out of the loop without adding '\r' to the input string
                        break;
                    }

                    // add up input-string
                    input += keyInfo.KeyChar;
                }

                // optional: enter was pressed - add a new line
                Console.WriteLine();

                // user pressed enter, do something with the input
                try
                {
                    if (input == "com")
                    {
                        // right option - do something
                    }
                    else
                    {
                        // wrong option - reset ConsoleKeyInfo + input
                        Console.WriteLine("\n" + input + " isn't an option.");
                        keyInfo = default(ConsoleKeyInfo);
                        input = string.Empty;
                        continue;
                    }
                }
                catch (Exception)
                {
                    // handle exceptions
                }
            }
        }
    }
}