Export (0) Print
Expand All

Console.TreatControlCAsInput Property

Gets or sets 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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static bool TreatControlCAsInput { get; set; }
/** @property */
public static boolean get_TreatControlCAsInput ()

/** @property */
public static void set_TreatControlCAsInput (boolean value)

public static function get TreatControlCAsInput () : boolean

public static function set TreatControlCAsInput (value : boolean)

Not applicable.

Property Value

true if CTRL+C is treated as ordinary input; otherwise, false.

Exception typeCondition

IOException

Unable to get or set the input mode of the console input buffer.

If the value of a get operation for the TreatControlCAsInput property is false and CTRL+C is pressed, then the pressed keys are not stored in the input buffer and the operating system terminates the currently executing process.

Caution noteCaution:

Use this property judiciously because setting it to true has such a dramatic effect. Most users expect CTRL+C to terminate a console application. If you disable the effect of CTRL+C, the user must remember to use CTRL+BREAK to terminate the application, which is a less familiar key combination.

The following example demonstrates the TreatControlCAsInput property.

// This example demonstrates the Console.ReadKey() method
using System;
using System.Text;

class Sample 
{
    public static void Main() 
    {
    ConsoleKeyInfo cki;
//               0        1         2         3         4         5         6
//               123456789012345678901234567890123456879012345678901234567890
    String m1 = "This example discovers the console and modifier keys " +
                "that you press.\n";
    String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, " +
                "and a console key.\nPress the Escape (Esc) key to quit: ";
    String m3 = "You pressed ";
    String m4 = " (character '{0}').";
    StringBuilder sb = new StringBuilder();   
//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and 
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect. 
//
    Console.TreatControlCAsInput = true;
    Console.WriteLine(m1);
    do 
    {
        Console.WriteLine(m2);
        sb.Length = 0;
        cki = Console.ReadKey(true);
        sb.Append(m3);
        if (cki.Modifiers != 0)
            {
            if ((cki.Modifiers & ConsoleModifiers.Alt) != 0)
                sb.Append("ALT+");
            if ((cki.Modifiers & ConsoleModifiers.Shift) != 0)
                sb.Append("SHIFT+");
            if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
                sb.Append("CTL+");
            }
        sb.Append(cki.Key.ToString());
        sb.AppendFormat(m4, cki.KeyChar);
        sb.AppendLine().AppendLine();
        Console.WriteLine(sb.ToString());
    } while (cki.Key != ConsoleKey.Escape);
// Note: This example requires the Escape (Esc) key.
    }
}
/*
This example produces results similar to following text:

This example discovers the console and modifier keys that you press.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed A (character 'a').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed SHIFT+A (character 'A').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed ALT+SHIFT+CTL+A (character ' ').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed Escape (character '?').
*/

// This example demonstrates the Console.ReadKey() method
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        ConsoleKeyInfo cki;
        //        0        1         2         3         4         5         6
        //        123456789012345678901234567890123456879012345678901234567890
        String m1 = "This example discovers the console and modifier keys "
            + "that you press.\n";
        String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, "
            + "and a console key.\nPress the Escape (Esc) key to quit: ";
        String m3 = "You pressed ";
        String m4 = " (character '{0}').";
        StringBuilder sb = new StringBuilder();
        //
        // The Console.TreatControlCAsInput property prevents this example from
        // ending if you press CTL+C, however all other operating system keys and 
        // shortcuts, such as ALT+TAB or the Windows Logo key, are still in
        // effect.
        //
        Console.set_TreatControlCAsInput(true);
        Console.WriteLine(m1);
        do {
            Console.WriteLine(m2);
            sb.set_Length(0);
            cki = Console.ReadKey(true);
            sb.Append(m3);
            if (!(cki.get_Modifiers().Equals((Int32)0))) {
                if (!((cki.get_Modifiers() & ConsoleModifiers.Alt).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("ALT+");
                }
                if (!((cki.get_Modifiers() & ConsoleModifiers.Shift).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("SHIFT+");
                }
                if (!((cki.get_Modifiers() & ConsoleModifiers.Control).ToString().
                    Equals(Convert.ToString(0)))) {
                    sb.Append("CTL+");
                }
            }
            sb.Append(cki.get_Key().ToString());
            sb.AppendFormat(m4, (System.Char)cki.get_KeyChar());
            sb.AppendLine().AppendLine();
            Console.WriteLine(sb.ToString());
        } while (!(cki.get_Key().Equals(ConsoleKey.Escape)));
        // Note: This example requires the Escape (Esc) key.
    } //main
} //Sample
/*
This example produces results similar to following text:

This example discovers the console and modifier keys that you press.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed A (character 'a').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed SHIFT+A (character 'A').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed ALT+SHIFT+CTL+A (character ' ').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the Escape (Esc) key to quit:
You pressed Escape (character '?').
*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft