閱讀英文

共用方式為


Console.CancelKeyPress 事件

定義

發生於同時按下 Control 輔助按鍵 (Ctrl) 和 C 主控台按鍵 (C) 或 Break 鍵 (Ctrl+C 或 Ctrl+Break) 時。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static event ConsoleCancelEventHandler? CancelKeyPress;
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static event ConsoleCancelEventHandler? CancelKeyPress;
C#
public static event ConsoleCancelEventHandler CancelKeyPress;

事件類型

屬性

範例

下列範例示範如何使用 CancelKeyPress 事件。 當您按下 Ctrl+C 時,讀取作業會中斷,並 myHandler 叫用事件處理常式。 進入事件處理常式時, ConsoleCancelEventArgs.Cancel 屬性為 false ,這表示當事件處理常式終止時,目前的進程將會終止。 不過,事件處理常式會將 ConsoleCancelEventArgs.Cancel 屬性設定為 true ,這表示進程不會終止,而且讀取作業將會繼續。

C#
using System;

class Sample
{
    public static void Main()
    {
        ConsoleKeyInfo cki;

        Console.Clear();

        // Establish an event handler to process key press events.
        Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);
        while (true)
        {
            Console.Write("Press any key, or 'X' to quit, or ");
            Console.WriteLine("CTRL+C to interrupt the read operation:");

            // Start a console read operation. Do not display the input.
            cki = Console.ReadKey(true);

            // Announce the name of the key that was pressed .
            Console.WriteLine($"  Key pressed: {cki.Key}\n");

            // Exit if the user pressed the 'X' key.
            if (cki.Key == ConsoleKey.X) break;
        }
    }

    protected static void myHandler(object sender, ConsoleCancelEventArgs args)
    {
        Console.WriteLine("\nThe read operation has been interrupted.");

        Console.WriteLine($"  Key pressed: {args.SpecialKey}");

        Console.WriteLine($"  Cancel property: {args.Cancel}");

        // Set the Cancel property to true to prevent the process from terminating.
        Console.WriteLine("Setting the Cancel property to true...");
        args.Cancel = true;

        // Announce the new value of the Cancel property.
        Console.WriteLine($"  Cancel property: {args.Cancel}");
        Console.WriteLine("The read operation will resume...\n");
    }
}
// The example displays output similar to the following:
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: J
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: Enter
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//
//    The read operation has been interrupted.
//      Key pressed: ControlC
//      Cancel property: False
//    Setting the Cancel property to true...
//      Cancel property: True
//    The read operation will resume...
//
//      Key pressed: Q
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: X

備註

此事件會與 和 System.ConsoleCancelEventArgs 搭配 System.ConsoleCancelEventHandler 使用。 事件 CancelKeyPress 可讓主控台應用程式攔截 Ctrl+C 訊號,讓事件處理常式可以決定是否要繼續執行或終止。 如需處理事件的詳細資訊,請參閱 處理和引發事件

當使用者按下 Ctrl+C 或 Ctrl+Break 時, CancelKeyPress 會引發事件並執行應用程式的 ConsoleCancelEventHandler 事件處理常式。 事件處理常式會傳遞具有兩個 ConsoleCancelEventArgs 實用屬性的物件:

  • SpecialKey,這可讓您判斷是否因為使用者按下 Ctrl+C 而叫用處理程式, (屬性值 ConsoleSpecialKey.ControlC 是) 還是 Ctrl+Break (屬性值是 ConsoleSpecialKey.ControlBreak) 。

  • Cancel,可讓您判斷應用程式應該如何回應使用者按下 Ctrl+C 或 Ctrl+Break。 根據預設, Cancel 屬性為 false ,這會導致程式在事件處理常式結束時終止。 變更其 屬性以 true 指定應用程式應該繼續執行。

提示

如果您的應用程式有簡單的需求,您可以使用 TreatControlCAsInput 屬性,而不是此事件。 藉由將此屬性設定為 false ,您可以確定如果使用者按下 Ctrl+C,應用程式一律會結束。 藉由將它設定為 true ,您可以確定按下 Ctrl+C 不會終止應用程式。

這個事件的事件處理常式會線上程集區執行緒上執行。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

另請參閱