In this article, you will learn about the Console.TreatControlCAsInput property in C# with its syntax, parameters, and examples.
What is Console.TreatControlCAsInput Property?
The property "Console.TreatControlCAsInput" may acquire or modify a value indicating whether the operating system interprets the Control modifier key and C console key (Ctrl+C) combination as ordinary input or as an interruption.
Syntax:
It has the following syntax:
public static bool TreatControlCAsInput { get; set; }
Property Value: When Ctrl+C is interpreted as regular input, this property yields a true value. If it is false, the program terminates if the input is Ctrl+C. This property will raise an IOException if the program cannot get or modify the console input buffer's input mode.
Functionality:
- Default Behaviour: Ctrl+C is interpreted as an interrupt signal by default when TreatControlCAsInput is set to false. Accordingly, the application terminates when you press Ctrl+C.
- Custom Behaviour: When TreatControlCAsInput is set to true, the key combination Ctrl+C is interpreted as standard input. It enables programmers to implement custom logic to handle the Ctrl+C shortcut, such as treating it like a command shortcut or ignoring it altogether.
- Control Flow: Modifying TreatControlCAsInput impacts the application's control flow. While setting it to false relies on the application terminating by default, setting it to true allows the application to handle Ctrl+C explicitly within its input processing logic.
- True: Ctrl+C is regarded as standard input when this setting is applied. It indicates that Ctrl+C may be handled by the application's main loop, just like any other key press.
- False: Ctrl+C functions as an interrupt signal when it set to false. As with pressing the close button or sending a SIGINT signal in Unix-like systems, this results in the application terminating instantly.
Behaviour:
I. Console.TreatControlCAsInput set to 'true'
Example-1:
Let us take an example to illustrate the Console.TreatControlCAsInput property when it is set to true in C#.
using System;
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Ctrl+C will be treated as input.");
// TreatControlCAsInput should be set to 'true'.
Console.TreatControlCAsInput = true;
Console.WriteLine("Press Ctrl+C to test:");
while (true)
{
ConsoleKeyInfo q = Console.ReadKey(true);
if (q.Modifiers == ConsoleModifiers.Control && q.Key == ConsoleKey.C)
{
Console.WriteLine("You pressed Ctrl+C.");
// Additional handling for Ctrl+C as input if needed
}
else
{
Console.WriteLine("You pressed the key is: " + q.KeyChar);
}
}
}
}
Output:
Ctrl+C will be treated as input.
Press Ctrl+C to test:
e r s
You pressed the key is: e
You pressed the key is:
You pressed the key is: r
You pressed the key is:
You pressed the key is: s
You pressed the key is:
Explanation:
In this case, Ctrl+C will be handled as ordinary input because of Console.TreatControlCAsInput is set to true. Pressing Ctrl+C will cause the program to print a message but won't terminate.
Example-2:
Let us take another example to illustrate the Console.TreatControlCAsInput property when it is set to true in C#.
using System;
class Demo
{
static void Main()
{
ConsoleKeyInfo q;
Console.TreatControlCAsInput = true;
Console.WriteLine("Press any key with combination of ALT, "+
"CTL, and Press the Esc to quit or SHIFT: \n");
do {
q = Console.ReadKey();
Console.Write("You pressed the key is: ");
if ((q.Modifiers & ConsoleModifiers.Shift) != 0)
Console.Write("SHIFT + ");
if ((q.Modifiers & ConsoleModifiers.Control) != 0)
Console.Write("CTRL + ");
if ((q.Modifiers & ConsoleModifiers.Alt) != 0)
Console.Write("ALT + ");
Console.WriteLine(q.Key.ToString());
} while (q.Key != ConsoleKey.Escape);
}
}
Output:
Press any key with a combination of ALT and CTL, and Press the Esc to quit or SHIFT:
D
You pressed the key is: SHIFT + D
You pressed the key is: Enter
S
You pressed the key is: SHIFT + S
You pressed the key is: Enter
a
You pressed the key is: A
You pressed the key is: Enter
Explanation:
With the help of this program, it is easy to recognize and display pressed keys along with modifier keys like SHIFT, CTRL , and ALT . It demonstrates how to use the Console.TreatControlCAsInput property by treating Ctrl+C as regular input.
II. Console.TreatControlCAsInput set to 'false'
Example:
Let us take another example to illustrate the Console.TreatControlCAsInput property when it is set to false in C#.
using System;
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Ctrl+C will be treated as interrupt.");
// TreatControlCAsInput should be set to 'false'.
Console.TreatControlCAsInput = false;
Console.WriteLine("Press Ctrl+C to test:");
while (true)
{
ConsoleKeyInfo q = Console.ReadKey(true);
if (q.Modifiers == ConsoleModifiers.Control && q.Key == ConsoleKey.C)
{
Console.WriteLine("Ctrl+C is treated as interrupt. Exiting...");
break;
}
else
{
Console.WriteLine("You pressed the key is: " + q.KeyChar);
}
}
}
}
Output:
Ctrl+C will be treated as an interrupt.
Press Ctrl+C to test:n g k s
You pressed the key is: n
You pressed the key is:
You pressed the key is: g
You pressed the key is:
You pressed the key is: k
You pressed the key is:
You pressed the key is: s
You pressed the key is:
Explanation:
In this example, Ctrl+C will be interpreted as an interrupt signal because the Console.TreatControlCAsInput is set to false. When Ctrl+C is pressed, the program will print a message and exit.