當前位置: 首頁>>代碼示例>>C#>>正文


C# ConsoleKeyInfo.Key屬性代碼示例

本文整理匯總了C#中System.ConsoleKeyInfo.Key屬性的典型用法代碼示例。如果您正苦於以下問題:C# ConsoleKeyInfo.Key屬性的具體用法?C# ConsoleKeyInfo.Key怎麽用?C# ConsoleKeyInfo.Key使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.ConsoleKeyInfo的用法示例。


在下文中一共展示了ConsoleKeyInfo.Key屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

class Example 
{
   public static void Main() 
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do 
      {
         cki = Console.ReadKey();
         Console.Write(" --- You pressed ");
         if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine(cki.Key.ToString());
       } while (cki.Key != ConsoleKey.Escape);
    }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//       
//       a --- You pressed A 
//       k --- You pressed ALT+K 
//       ► --- You pressed CTL+P 
//         --- You pressed RightArrow 
//       R --- You pressed SHIFT+R 
//                --- You pressed CTL+I 
//       j --- You pressed ALT+J 
//       O --- You pressed SHIFT+O 
//       § --- You pressed CTL+U
開發者ID:.NET開發者,項目名稱:System,代碼行數:37,代碼來源:ConsoleKeyInfo.Key

示例2: Main

//引入命名空間
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);
  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:37,代碼來源:ConsoleKeyInfo.Key


注:本文中的System.ConsoleKeyInfo.Key屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。