Console.TreatControlCAsInput Property in C# with examples - GeeksforGeeks

14 Mar, 2019
Console.TreatControlCAsInput Property is used to get or set a value indicating whether the combination of the Control modifier key and C console key (Ctrl+C) is treated as ordinary input or as an interruption that is handled by the operating system.
Syntax:
public static bool TreatControlCAsInput { get; set; }
Property Value: This property returns true if Ctrl+C is treated as ordinary input. Otherwise false, it means if the input is Ctrl+C then the program is terminated.
Exception: This property will give IOException if the program is not able to get or set the input mode of the console input buffer.
Below programs illustrate the use of the above-discussed property:
Example 1:
// C# program to illustrate the use of// Console.TreatControlCAsInput Propertyusing 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);              }}
Output:
Example 2:
// C# program to illustrate the use of// Console.TreatControlCAsInput Propertyusing 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);              }}
Output:
When false
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);              }}
Output :
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.

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.console.treatcontrolcasinput?view=netframework-4.7.2
CSharp-Console-Class
CSharp-method
Picked
C#
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
Top 50 C# Interview Questions & Answers
Partial Classes in C#
HashSet in C# with Examples
C# | Inheritance
C# | How to insert an element in an Array?
C# | List Class
Lambda Expressions in C#
C# | Generics - Introduction
What is Regular Expression in C#?