Console.TreatControlCAsInput Property in C# with examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Console.TreatControlCAsInput Property
using 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);
          
    }
}

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Console.TreatControlCAsInput Property
using 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);
          
    }
}

chevron_right


Output:

When false

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
          
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

2nd year student of Information Technology JADAVPUR UNIVERSITY

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.