当前位置: 首页>>代码示例>>C#>>正文


C# ConsoleKeyInfo.KeyChar属性代码示例

本文整理汇总了C#中System.ConsoleKeyInfo.KeyChar属性的典型用法代码示例。如果您正苦于以下问题:C# ConsoleKeyInfo.KeyChar属性的具体用法?C# ConsoleKeyInfo.KeyChar怎么用?C# ConsoleKeyInfo.KeyChar使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.ConsoleKeyInfo的用法示例。


在下文中一共展示了ConsoleKeyInfo.KeyChar属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      // Configure console.
      Console.BufferWidth = 80;
      Console.WindowWidth = Console.BufferWidth;
      Console.TreatControlCAsInput = true;
      
      string inputString = String.Empty;
      ConsoleKeyInfo keyInfo;

      Console.WriteLine("Enter a string. Press <Enter> or Esc to exit.");
      do {
         keyInfo = Console.ReadKey(true);
         // Ignore if Alt or Ctrl is pressed.
         if ((keyInfo.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt) 
            continue;
         if ((keyInfo.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
            continue; 
         // Ignore if KeyChar value is \u0000.
         if (keyInfo.KeyChar == '\u0000') continue;
         // Ignore tab key.
         if (keyInfo.Key == ConsoleKey.Tab) continue;
         // Handle backspace.
         if (keyInfo.Key == ConsoleKey.Backspace) {
            // Are there any characters to erase?
            if (inputString.Length >= 1) { 
               // Determine where we are in the console buffer.
               int cursorCol = Console.CursorLeft - 1;
               int oldLength = inputString.Length;
               int extraRows = oldLength / 80;

               inputString = inputString.Substring(0, oldLength - 1);
               Console.CursorLeft = 0;
               Console.CursorTop = Console.CursorTop - extraRows;
               Console.Write(inputString + new String(' ', oldLength - inputString.Length));
               Console.CursorLeft = cursorCol;
            }
            continue;
         }
         // Handle Escape key.
         if (keyInfo.Key == ConsoleKey.Escape) break;
         // Handle key by adding it to input string.
         Console.Write(keyInfo.KeyChar);
         inputString += keyInfo.KeyChar;
      } while (keyInfo.Key != ConsoleKey.Enter);
      Console.WriteLine("\n\nYou entered:\n    {0}", 
                        String.IsNullOrEmpty(inputString) ? "<nothing>" : inputString);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:54,代码来源:ConsoleKeyInfo.KeyChar

示例2: Main

//引入命名空间
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'); 
  }    
}
开发者ID:C#程序员,项目名称:System,代码行数:25,代码来源:ConsoleKeyInfo.KeyChar


注:本文中的System.ConsoleKeyInfo.KeyChar属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。