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.
参考:
相关用法
- CSS transition-property用法及代码示例
- C# Uri.Fragment用法及代码示例
- C# Uri.DnsSafeHost用法及代码示例
- HTML DOM lang用法及代码示例
- HTML Style backgroundClip用法及代码示例
- CSS border-top-width用法及代码示例
- HTML Style columnGap用法及代码示例
- HTML Style boxSizing用法及代码示例
- CSS isolation属性用法及代码示例
- HTML Style borderImageSource用法及代码示例
- HTML Style wordBreak用法及代码示例
- CSS column-rule-width用法及代码示例
- CSS word-spacing用法及代码示例
- CSS margin-top用法及代码示例
- HTML Style pageBreakBefore用法及代码示例
- HTML Style borderImageOutset用法及代码示例
- HTML Style maxHeight用法及代码示例
- HTML DOM characterSet用法及代码示例
- CSS grid属性用法及代码示例
- HTML Style columnRuleWidth用法及代码示例
- HTML Style transitionTimingFunction用法及代码示例
- CSS font-size-adjust用法及代码示例
- HTML Style zIndex用法及代码示例
注:本文由纯净天空筛选整理自SoumikMondal大神的英文原创作品 Console.TreatControlCAsInput Property in C# with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。