Console.Treatcontrolcasinput Property In C#

In this guide, you will discover the Console.TreatControlCAsInput attribute in C# along with its syntax, arguments, and illustrations.

What is Console.TreatControlCAsInput Property?

The "Console.TreatControlCAsInput" property can be used to obtain or change a value that signifies if the Control modifier key and C console key (Ctrl+C) combination is perceived by the operating system as regular input or as an interruption.

Syntax:

It has the following syntax:

Example

public static bool TreatControlCAsInput { get; set; }

When considered as standard input, this property returns a true value, and the program will stop execution if Ctrl+C is entered as input when it is false. An IOException will be triggered if the program encounters issues accessing or altering the input mode of the console input buffer.

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.
  • Behaviour:

  • 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.
  • I. Console.TreatControlCAsInput set to 'true'

Example-1:

Let's consider an example to demonstrate the Console.TreatControlCAsInput property in C# when it is configured as true.

Example

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:

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 scenario, pressing Ctrl+C will function as regular input due to the setting of Console.TreatControlCAsInput to true. When Ctrl+C is pressed, the program will display a message without ending the execution.

Example-2:

Let's consider another scenario to demonstrate the behavior of the Console.TreatControlCAsInput property when it's enabled in C#.

Example

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:

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 this software, identifying and showcasing pressed keys, including modifier keys such as SHIFT, CTRL, and ALT, is straightforward. It illustrates the utilization of the Console.TreatControlCAsInput attribute to handle Ctrl+C as standard input.

II. Console.TreatControlCAsInput set to 'false'

Example:

Let's consider another scenario to explain the behavior of the Console.TreatControlCAsInput property in C# when its value is false.

Example

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:

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 instance, pressing Ctrl+C will trigger an interrupt signal due to the Console.TreatControlCAsInput property being configured as false. Upon Ctrl+C input, the application will display a message before terminating.

Input Required

This code uses input(). Please provide values below: