'esc' key ?????????

I want to exit a c# command line program that i made by pressing only the 'esc' key. How can i do that thanks (make it simple!!!!)


Answer this question

'esc' key ?????????

  • tgbt

    Hi,

    not sure how your program works exactly, but see if you find the following piece of code helpful:

    static void Main(string[] args)
    {
    Console.WriteLine("Waiting for escape key...");
    while (true)
    {
    ConsoleKeyInfo key = Console.ReadKey();
    if (key.Key == ConsoleKey.Escape)
    {
    break;
    }
    }
    }

    Andrej



  • millertime408

    ahmedilyas wrote:

    no offense V.Tortola ! :-)



    Don't worry, learn new and/or more efficient ways is always wellcome :D

    Regards.


  • NiklasECG2

    Yes, that's possible, by key interception. Try the following code:

    static void Main(string[] args)
    {
       
    Console.WriteLine("Type your password and press ENTER...");
       
    StringBuilder passwordBuilder = new StringBuilder();
       
    while (true)
       
    {
           
    ConsoleKeyInfo key = Console.ReadKey(true);
           
    if (key.Key == ConsoleKey.Enter)
           
    {
               
    Console.WriteLine("\r\nYour password: " + passwordBuilder.ToString());
               
    Console.ReadKey();
               
    break;
           
    }
           
    passwordBuilder.Append(key.KeyChar);
           
    Console.Write("*");
       
    }
    }

    Andrej



  • HCTwinJava

    the last one would be a better solution than checking the actual key code value :-) It's better to do it the .NET way

    no offense V.Tortola ! :-)



  • Fwank79

    It could be TexBox but In need to use it in the command line (console)!!!

  • Dr.Virusi

    In a TextBox, config the PasswordChar to a asterisk ;)

    Regards.



  • Greg Leininger

    thank you...

    this is the code that i'm going to use:

    Console.WriteLine("\t\tPress Esc to exit or ENTER to continue");

    ConsoleKeyInfo key = Console.ReadKey();

    if (key.Key == ConsoleKey.Escape)

    Environment.Exit(-1);

    once again thank's..



  • ly4587

     

    Great work!!! :)

     

    thanks....



  • Luis Neves

    In the key press event of the main form ...


    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == (char)27) Application.Exit();
    }

     

     

    Regards.



  • 'esc' key ?????????