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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。