Javatpoint Logo
Javatpoint Logo

Console.TreatControlCAsInput Property in C#

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:

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:

  1. 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.
  2. 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.
  3. 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:

  1. 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.
  2. 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 us take an example to illustrate the Console.TreatControlCAsInput property when it is set to true in C#.

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#.

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#.

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA