Open In App

Console.TreatControlCAsInput Property in C# with examples

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

csharp




// 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 CTRL+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: 

csharp




// 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 CTRL+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("CTRL + ");
 
            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 

csharp




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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads