Use ConsoleKey to get input : ConsoleKey : Development C# Examples


C# Examples » Development » ConsoleKey »

 

Use ConsoleKey to get input









    
using  System;
using  System.Collections.Generic;

class  MainClass
{
        public  static  void  Main()
        {
                ConsoleKeyInfo  key;
                Console.WriteLine("Process  input  until  the  user  enters  Alt-X  or  Alt-x");
                
                do
                {
                        key  =  Console.ReadKey(true);

                        Console.WriteLine(key);
                        if  (key.Key  ==  ConsoleKey.F1)
                        {
                              Console.WriteLine("F1");
                        }

                        //  Handle  backspace.
                        if  (key.Key  ==  ConsoleKey.Backspace)
                        {
                                Console.WriteLine("Backspace");
                        }
                        //  Handle  Escape.
                        else  if  (key.Key  ==  ConsoleKey.Escape)
                        {
                                Console.WriteLine("Escape");
                        }
                        //  Handle  character  input.
                        else  if  (key.Key  >=  ConsoleKey.A  &&  key.Key  <=  ConsoleKey.Z)
                        {
                                Console.WriteLine(">=ConsoleKey.A  &&  <=  ConsoleKey.Z");
                        }
                }  while  (key.Key  !=  ConsoleKey.X  ||  key.Modifiers  !=  ConsoleModifiers.Alt);

        }
}
    
   
  
   



Output

Process input until the user enters Alt-X or Alt-x
System.ConsoleKeyInfo
>=ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && =ConsoleKey.A && 


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» ConsoleKey