0

I need a way so user can exit the program by pressing escape button, so CancelKeyPress event doesn't work here. It is very important that the user can quit at any time they want.

I really have no idea how to do it as I am beginner, so I count on you. Thanks in advance.

WizzzyTM
  • 11
  • 1
  • 1
    Hi and Welcome to SO. If you haven't already, please take the [tour]. It's also always a good idea to keep yourself updated about [ask]. – Fildor Dec 13 '22 at 15:03
  • 1
    You have to be clear what you want. You need to define your problem better. E.g. which programming language should be used. – Florian Dec 13 '22 at 15:05
  • 1
    "It is very important that the user can quit at any time they want." It really depends on what your program is doing! You can periodically check if the there is a key in the buffer with [Console.KeyAvailable](https://learn.microsoft.com/en-us/dotnet/api/system.console.keyavailable?source=recommendations&view=net-7.0), then actually extract and check it with [Console.ReadKey()](https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-7.0). – Idle_Mind Dec 13 '22 at 15:06
  • 1
    @Fildor, Author states, "by pressing escape button, so CancelKeyPress event doesn't work here", so you guys close it with a solution using CancelKeyPress which fires on Ctrl-C and/or Ctrl-Break... – Idle_Mind Dec 13 '22 at 16:38
  • 2
    @Idle_Mind I'll re-open it, but that comment was added after it was closed. – LarsTech Dec 13 '22 at 16:43
  • 2
    You have to replace *every* call to Console.ReadLine() in your program. With [this](https://stackoverflow.com/questions/19734531/how-do-i-interrupt-not-using-thread-console-readline-when-a-key-is-pressed/19735111#19735111), replace F4 with Escape and return false with Environment.Exit() – Hans Passant Dec 13 '22 at 17:53
  • 1
    @Idle_Mind and the question has been edited since first comment. At the time I cast my vote, it was close worthy. – Fildor Dec 13 '22 at 18:08

2 Answers2

0

What i do when i make a Console application is a switch statement for exit.


switch (choice)
{
  case "1":
    util.Option();
    break;
  .
  .
  .

   case "q":
     System.Environment.Exit(1);

   default:
     break;             
}

System.Environment.Exit(1);can help you exit your console.

gouravm
  • 301
  • 1
  • 12
0

Based on your description, I think you have solved the following code:

static void Main(string[] args)
{
    ConsoleKeyInfo CurrentInputKey;

    while(true)
    {
        // Your Other Codes
        Console.WriteLine("\rHello C# Developer.");
        CurrentInputKey = Console.ReadKey();

        if(CurrentInputKey != null && CurrentInputKey.Key == ConsoleKey.Escape)
        {
            Console.WriteLine("Exit ...");
            System.Environment.Exit(0);
        }
    }
}
KeyvanSFX
  • 113
  • 1
  • 11
  • hi, in which place should I put my program so it works? – WizzzyTM Dec 13 '22 at 17:38
  • It should be inside the While loop so that if the user presses the ESC key, the program will close immediately – KeyvanSFX Dec 13 '22 at 17:47
  • "immediately"...not exactly. It will exit the next time those statements are executed and escape was pressed. If he inserts his program inside the while loop, and the code has BLOCKING CALLS, then it definitely won't exit immediately. We don't actually know what he is doing because he posted zero code and/or explanation of his program... – Idle_Mind Dec 13 '22 at 18:03
  • @Idle_Mind Yes, you are right, I meant **immediately** that it does not have a tangible delay, which is acceptable for simple projects (where the time factor is not so important). – KeyvanSFX Dec 13 '22 at 19:21