英語で読む

次の方法で共有


Console.TreatControlCAsInput プロパティ

定義

Control 修飾キーと C コンソール キーの組み合わせ (Ctrl + C) を、通常の入力として扱うか、オペレーティング システムにより処理される割り込みとして扱うかを示す値を取得または設定します。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static bool TreatControlCAsInput { get; set; }
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool TreatControlCAsInput { get; set; }
C#
public static bool TreatControlCAsInput { get; set; }

プロパティ値

Ctrl + C を通常の入力として扱う場合は true。それ以外の場合は false

属性

例外

コンソールの入力バッファーの入力モードを取得または設定できません。

TreatControlCAsInputプロパティの例を次に示します。

C#
using System;

class Example
{
   public static void Main()
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do
      {
         cki = Console.ReadKey();
         Console.Write(" --- You pressed ");
         if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine(cki.Key.ToString());
       } while (cki.Key != ConsoleKey.Escape);
    }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//
//       a --- You pressed A
//       k --- You pressed ALT+K
//       ► --- You pressed CTL+P
//         --- You pressed RightArrow
//       R --- You pressed SHIFT+R
//                --- You pressed CTL+I
//       j --- You pressed ALT+J
//       O --- You pressed SHIFT+O
//       § --- You pressed CTL+U

注釈

プロパティの TreatControlCAsInput 値が で false 、Ctrl + C キーが押された場合、押されたキーは入力バッファーに格納されず、オペレーティング システムは現在実行中のプロセスを終了します。 これが既定値です。

注意事項

このプロパティを に設定 true すると、このような劇的な効果があるため、慎重に使用してください。 ほとんどのユーザーは、Ctrl + C キーを押してコンソール アプリケーションを終了することを期待しています。 Ctrl + C の効果を無効にした場合、ユーザーは Ctrl + Break キーを使用してアプリケーションを終了する必要があります。これは、あまり使い慣れていないキーの組み合わせです。

適用対象

製品 バージョン
.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

こちらもご覧ください