'esc' key ?????????  
Author Message
schmack





PostPosted: Visual C# Language, 'esc' key ????????? Top

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!!!!)


Visual C#10  
 
 
V.Tortola





PostPosted: Visual C# Language, 'esc' key ????????? Top

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.



 
 
Andrej Tozon





PostPosted: Visual C# Language, 'esc' key ????????? Top

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



 
 
ahmedilyas





PostPosted: Visual C# Language, 'esc' key ????????? Top

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 ! :-)



 
 
V.Tortola





PostPosted: Visual C# Language, 'esc' key ????????? Top

no offense V.Tortola ! :-)



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

Regards.


 
 
schmack





PostPosted: Visual C# Language, 'esc' key ????????? Top

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..



 
 
V.Tortola





PostPosted: Visual C# Language, 'esc' key ????????? Top

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

Regards.



 
 
schmack





PostPosted: Visual C# Language, 'esc' key ????????? Top

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

 
 
Andrej Tozon





PostPosted: Visual C# Language, 'esc' key ????????? Top

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



 
 
schmack





PostPosted: Visual C# Language, 'esc' key ????????? Top

 

Great work!!! :)

 

thanks....