Csharp/C Sharp by API/System/ConsoleKeyInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ConsoleKeyInfo.Key

 
using System;
using System.Collections.Generic;
class MainClass
{
    public static void Main()
    {
        ConsoleKeyInfo key;
        List<char> input = new List<char>(); 
        do{
          key = Console.ReadKey(true);
          if (key.Key == ConsoleKey.F1)
          {
             Console.WriteLine("F1");
          }
          if (key.Key == ConsoleKey.Backspace)
          {
              if (input.Count > 0)
              {
                  input.RemoveAt(input.Count - 1);
                  Console.Write(key.KeyChar);
              }
          }else if (key.Key == ConsoleKey.Escape){
                Console.Clear();
                Console.WriteLine("Input: {0}\n\n",new String(input.ToArray()));
                input.Clear();
          }else if (key.Key >= ConsoleKey.A && key.Key <= ConsoleKey.Z){
                input.Add(key.KeyChar); 
                Console.Write(key.KeyChar);
           }
      } while (key.Key != ConsoleKey.X || key.Modifiers != ConsoleModifiers.Alt);
  }
}


ConsoleKeyInfo.KeyChar

 
  
using System;  
  
class MainClass {    
  public static void Main() {  
    ConsoleKeyInfo keypress; 
  
    Console.WriteLine("Enter keystrokes. Enter Q to stop."); 
  
    do { 
      keypress = Console.ReadKey(); // read keystrokes 
     
      Console.WriteLine(" Your key is: " + keypress.KeyChar);  
 
      // Check for modifier keys. 
      if((ConsoleModifiers.Alt & keypress.Modifiers) != 0) 
        Console.WriteLine("Alt key pressed."); 
      if((ConsoleModifiers.Control & keypress.Modifiers) != 0) 
        Console.WriteLine("Control key pressed."); 
      if((ConsoleModifiers.Shift & keypress.Modifiers) != 0) 
        Console.WriteLine("Shift key pressed."); 
 
    } while(keypress.KeyChar != "Q"); 
  }    
}


ConsoleKeyInfo.Modifiers

 
using System;
using System.Collections.Generic;
class MainClass
{
    public static void Main()
    {
        ConsoleKeyInfo key;
        List<char> input = new List<char>(); 
        do{
          key = Console.ReadKey(true);
          if (key.Key == ConsoleKey.F1)
          {
             Console.WriteLine("F1");
          }
          if (key.Key == ConsoleKey.Backspace)
          {
              if (input.Count > 0)
              {
                  input.RemoveAt(input.Count - 1);
                  Console.Write(key.KeyChar);
              }
          }else if (key.Key == ConsoleKey.Escape){
                Console.Clear();
                Console.WriteLine("Input: {0}\n\n",new String(input.ToArray()));
                input.Clear();
          }else if (key.Key >= ConsoleKey.A && key.Key <= ConsoleKey.Z){
                input.Add(key.KeyChar); 
                Console.Write(key.KeyChar);
           }
      } while (key.Key != ConsoleKey.X || key.Modifiers != ConsoleModifiers.Alt);
  }
}