Skip to content
Related Articles

Related Articles

Improve Article
Console.TreatControlCAsInput Property in C# with examples
  • Last Updated : 14 Mar, 2019

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:




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

Output:

Example 2:




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

Output:

When false




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

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
Recommended Articles
Page :