当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Console.TreatControlCAsInput用法及代码示例


Console.TreatControlCAsInput 属性用于获取或设置一个值,该值指示 Control 修饰键和 C 控制台键 (Ctrl+C) 的组合是被视为普通输入还是被操作系统处理的中断。

用法:

public static bool TreatControlCAsInput { get; set; }

属性值:如果 Ctrl+C 被视为普通输入,则此属性返回 true。否则为 false,表示如果输入是 Ctrl+C,则程序终止。

Exception:如果程序无法获取或设置控制台输入缓冲区的输入模式,则此属性将给出 IOException。

以下示例程序旨在说明上述属性的使用:



范例1:


// C# program to illustrate the use of
// Console.TreatControlCAsInput Property
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        ConsoleKeyInfo c;
  
        // Prevent program from terminating 
        // if CTL+C is pressed.
        Console.TreatControlCAsInput = true;
  
        Console.WriteLine("Press any key with combination of CTL, "+
                    "ALT, and SHIFT or Press the Esc to quit:\n");
  
        do {
  
            c = Console.ReadKey();
            Console.Write(" - pressed key is ");
  
            // only prints the pressed keys
            Console.WriteLine(c.Key.ToString());
  
          // condition for Exit
        } while (c.Key != ConsoleKey.Escape);
          
    }
}

输出:

范例2:


// C# program to illustrate the use of
// Console.TreatControlCAsInput Property
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        ConsoleKeyInfo c;
  
        // Prevent program from terminating 
        // if CTL+C is pressed.
        Console.TreatControlCAsInput = true;
  
        Console.WriteLine("Press any key with combination of CTL, "+
                     "ALT, and SHIFT or Press the Esc to quit:\n");
  
        do {
  
            c = Console.ReadKey();
            Console.Write("pressed key is ");
  
            // conditions-
            if ((c.Modifiers & ConsoleModifiers.Shift) != 0)
                Console.Write("SHIFT + ");
  
            if ((c.Modifiers & ConsoleModifiers.Control) != 0)
                Console.Write("CTL + ");
  
            if ((c.Modifiers & ConsoleModifiers.Alt) != 0)
                Console.Write("ALT + ");
  
            // prints the pressed keys
            Console.WriteLine(c.Key.ToString());
             
          // condition for Exit
        } while (c.Key != ConsoleKey.Escape);
          
    }
}

输出:

假的时候


using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        ConsoleKeyInfo c;
  
        // Prevent program from terminating 
        // if CTL+C is pressed.
        Console.TreatControlCAsInput = false;
  
        Console.WriteLine("Press any key with combination of CTL,"+
                  " ALT, and SHIFT or Press the Esc to quit:\n");
  
        do {
            c = Console.ReadKey();
            Console.Write(" - pressed key is ");
  
            // only prints the pressed keys
            Console.WriteLine(c.Key.ToString());
              
          // condition for Exit
        } while (c.Key != ConsoleKey.Escape);
          
    }
}

输出:

Press any key with combination of CTL, ALT, and SHIFT or Press the Esc to quit:

a - pressed key is A
b - pressed key is B
g - pressed key is G

// Here after these input we press Ctrl+C, 
// then the program is terminated.

参考:




相关用法


注:本文由纯净天空筛选整理自SoumikMondal大神的英文原创作品 Console.TreatControlCAsInput Property in C# with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。