C# Terminating the program with a key combo without pressing enter while in read

1.2k Views Asked by At

Now here is the situation we are in:

     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;

what i want to do with this is when i press alt+f1 the program will terminate itself however because the program waits for some input from me to write even with the working version (without the alt part) it wants me to type the things then press enter, which i dont want. how does one handlde this??

2

There are 2 best solutions below

6
On

First of all, please consider using loops instead of goto, as gotos are dangerous. Why? Have a look here: 'Goto' is this bad?

To solve your problem you can use the ConsoleKeyInfo class in combination with the Console.ReadKey() method to get information about single key presses. With this you can check for any key-combination right before adding up the single characters to a string. A working example could look like:

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
                }
            }
        }
    }
}
6
On
        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);

        }

that code will help you with your problem, just be aware that when you reading a line by char's you will need to handle things like backspace