英語で読む

次の方法で共有


Console.CancelKeyPress イベント

定義

Control の修飾子キー (Ctrl) と C コンソール キー (c) または中断キーが同時にプッシュされた場合 (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.Canceltrue設定します。これは、プロセスが終了せず、読み取り操作が再開されることを意味します。

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.ConsoleCancelEventHandlerSystem.ConsoleCancelEventArgs組み合わせて使用されます。 イベント CancelKeyPress を使用すると、コンソール アプリケーションは Ctrl + C シグナルをインターセプトして、イベント ハンドラーが実行を続行するか終了するかを決定できます。 イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

ユーザーが Ctrl + C キーまたは Ctrl + Break キーを押すと、 CancelKeyPress イベントが発生し、アプリケーションの ConsoleCancelEventHandler イベント ハンドラーが実行されます。 イベント ハンドラーには、次の ConsoleCancelEventArgs 2 つの便利なプロパティを持つ オブジェクトが渡されます。

  • 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

こちらもご覧ください