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??
First of all, please consider using loops instead of
goto
, asgoto
s are dangerous. Why? Have a look here: 'Goto' is this bad?To solve your problem you can use the
ConsoleKeyInfo
class in combination with theConsole.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: