0

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

     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 部分)它也希望我输入这些内容按回车,我不想要。如何处理这个?

4

2 回答 2

0

首先,请考虑使用循环而不是s goto,因为gotos 很危险。为什么?看看这里:'Goto' 这很糟糕吗?

要解决您的问题,您可以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
                }
            }
        }
    }
}
于 2018-08-01T20:52:16.690 回答
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);

        }

该代码将帮助您解决问题,请注意,当您按字符读取一行时,您将需要处理退格等事情

于 2018-08-01T15:12:24.710 回答