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