Console.TreatControlCAsInput Property in C# with Examples

The Console.TreatControlCAsInput property in C# is a Boolean property that determines whether the Ctrl+C key combination is treated as ordinary input or as an interruption signal handled by the operating system.

By default, pressing Ctrl+C terminates a console application. However, setting this property to true allows the application to capture Ctrl+C as regular input, preventing automatic termination.

Syntax

Following is the syntax for setting and getting the Console.TreatControlCAsInput property

// Setting the property
Console.TreatControlCAsInput = true;  // or false

// Getting the property value
bool treatAsInput = Console.TreatControlCAsInput;

Default Behavior vs Custom Handling

Ctrl+C Handling Behavior Default (false) TreatControlCAsInput = false Ctrl+C terminates the application immediately Custom (true) TreatControlCAsInput = true Ctrl+C treated as regular input character

Using TreatControlCAsInput for Custom Input Handling

This example demonstrates how to capture Ctrl+C as input and handle it within the application logic

Example

using System;

class Program {
   static void Main() {
      Console.TreatControlCAsInput = true;
      Console.WriteLine("Press any key (Ctrl+C will not terminate). Type 'quit' to exit:");
      
      ConsoleKeyInfo keyInfo;
      string input = "";
      
      do {
         keyInfo = Console.ReadKey(true);
         
         if (keyInfo.Key == ConsoleKey.C && keyInfo.Modifiers == ConsoleModifiers.Control) {
            Console.WriteLine("\nCtrl+C detected! But application continues...");
         } else if (keyInfo.Key == ConsoleKey.Enter) {
            Console.WriteLine("\nYou entered: " + input);
            if (input.ToLower() == "quit") break;
            input = "";
         } else {
            input += keyInfo.KeyChar;
            Console.Write(keyInfo.KeyChar);
         }
      } while (true);
      
      Console.WriteLine("Application terminated normally.");
   }
}

The output of the above code when user presses various keys including Ctrl+C

Press any key (Ctrl+C will not terminate). Type 'quit' to exit:
hello
You entered: hello

Ctrl+C detected! But application continues...
world
You entered: world
quit
You entered: quit
Application terminated normally.

Reading Multiple Lines with Ctrl+C Protection

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      Console.TreatControlCAsInput = true;
      Console.WriteLine("Enter multiple lines of text. Type 'END' to finish:");
      Console.WriteLine("(Ctrl+C will not interrupt the input process)");
      
      List<string> lines = new List<string>();
      string line;
      
      while ((line = ReadLineWithCtrlCProtection()) != "END") {
         lines.Add(line);
         Console.WriteLine($"Line {lines.Count} saved: {line}");
      }
      
      Console.WriteLine("\nAll lines entered:");
      for (int i = 0; i < lines.Count; i++) {
         Console.WriteLine($"{i + 1}: {lines[i]}");
      }
   }
   
   static string ReadLineWithCtrlCProtection() {
      string result = "";
      ConsoleKeyInfo key;
      
      while ((key = Console.ReadKey(true)).Key != ConsoleKey.Enter) {
         if (key.Key == ConsoleKey.C && key.Modifiers == ConsoleModifiers.Control) {
            Console.Write("[Ctrl+C ignored] ");
         } else if (key.Key == ConsoleKey.Backspace && result.Length > 0) {
            result = result.Substring(0, result.Length - 1);
            Console.Write("\b \b");
         } else if (!char.IsControl(key.KeyChar)) {
            result += key.KeyChar;
            Console.Write(key.KeyChar);
         }
      }
      Console.WriteLine();
      return result;
   }
}

The output of the above code demonstrates protected input handling

Enter multiple lines of text. Type 'END' to finish:
(Ctrl+C will not interrupt the input process)
Hello World
Line 1 saved: Hello World
This is line 2[Ctrl+C ignored] with protection
Line 2 saved: This is line 2 with protection
END

All lines entered:
1: Hello World
2: This is line 2 with protection

Toggling TreatControlCAsInput During Runtime

Example

using System;

class Program {
   static void Main() {
      Console.WriteLine("Initial TreatControlCAsInput: " + Console.TreatControlCAsInput);
      
      Console.WriteLine("<br>1. Default behavior - Ctrl+C will terminate (if you try)");
      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
      
      Console.TreatControlCAsInput = true;
      Console.WriteLine("<br>2. Custom behavior - Ctrl+C treated as input");
      Console.WriteLine("Current TreatControlCAsInput: " + Console.TreatControlCAsInput);
      
      Console.WriteLine("Press Ctrl+C or any other key (ESC to continue):");
      ConsoleKeyInfo key;
      do {
         key = Console.ReadKey(true);
         if (key.Key == ConsoleKey.C && key.Modifiers == ConsoleModifiers.Control) {
            Console.WriteLine("Ctrl+C captured as input!");
         } else if (key.Key != ConsoleKey.Escape) {
            Console.WriteLine($"Key pressed: {key.Key}");
         }
      } while (key.Key != ConsoleKey.Escape);
      
      Console.TreatControlCAsInput = false;
      Console.WriteLine("<br>3. Restored default behavior");
      Console.WriteLine("Final TreatControlCAsInput: " + Console.TreatControlCAsInput);
   }
}

The output of the above code shows the property state changes

Initial TreatControlCAsInput: False

1. Default behavior - Ctrl+C will terminate (if you try)
Press any key to continue...

2. Custom behavior - Ctrl+C treated as input
Current TreatControlCAsInput: True
Press Ctrl+C or any other key (ESC to continue):
Ctrl+C captured as input!
Key pressed: A
Key pressed: B

3. Restored default behavior
Final TreatControlCAsInput: False

Key Considerations

  • Setting this property to true disables the default Ctrl+C termination behavior.

  • Only affects Ctrl+C combination ? other control sequences like Ctrl+Break still work normally.

  • Useful for applications that need to capture all user input without interruption.

  • The property can be toggled during runtime as needed.

Conclusion

The Console.TreatControlCAsInput property provides fine-grained control over Ctrl+C handling in console applications. By setting it to true, developers can create robust input systems that capture all keystrokes, including the traditionally reserved Ctrl+C combination, allowing for more sophisticated user interaction patterns.

Updated on: 2026-03-17T07:04:36+05:30

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements