ConsoleKeyInfoとは? わかりやすく解説

ConsoleKeyInfo コンストラクタ

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定した文字、コンソール キー、および修飾子キーを使用して、ConsoleKeyInfo 構造体の新しいインスタンスを初期化します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    keyChar As Char, _
    key As ConsoleKey, _
    shift As Boolean, _
    alt As Boolean, _
    control As Boolean _
)
Dim keyChar As Char
Dim key As ConsoleKey
Dim shift As Boolean
Dim alt As Boolean
Dim control As Boolean

Dim instance As New ConsoleKeyInfo(keyChar,
 key, shift, alt, control)
public ConsoleKeyInfo (
    char keyChar,
    ConsoleKey key,
    bool shift,
    bool alt,
    bool control
)
public:
ConsoleKeyInfo (
    wchar_t keyChar, 
    ConsoleKey key, 
    bool shift, 
    bool alt, 
    bool control
)
public ConsoleKeyInfo (
    char keyChar, 
    ConsoleKey key, 
    boolean shift, 
    boolean alt, 
    boolean control
)
public function ConsoleKeyInfo (
    keyChar : char, 
    key : ConsoleKey, 
    shift : boolean, 
    alt : boolean, 
    control : boolean
)

パラメータ

keyChar

key パラメータに対応する Unicode 文字。

key

keyChar パラメータに対応するコンソール キー。

shift

Shift キーが押されたことを示す場合は true。それ以外の場合は false。

alt

Alt キーが押されたことを示す場合は true。それ以外の場合は false。

control

Ctrl キーが押されたことを示す場合は true。それ以外の場合は false。

例外例外
例外の種類条件

ArgumentOutOfRangeException

key パラメータの数値が、0 未満か 255 より大きい数値です。

解説解説

この型は、ユーザーが作成することを意図した型ではありません。代わりに、この型は、Console.ReadKey メソッドの呼び出しに対する応答としてユーザーに返されます。

ConsoleKeyInfo 型では、Shift、Alt、Ctrl の各修飾子キーで左右のどちらのキーが押されたかは指定しません。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ConsoleKeyInfo 構造体
ConsoleKeyInfo メンバ
System 名前空間

ConsoleKeyInfo プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

ConsoleKeyInfo 構造体
System 名前空間
ConsoleModifiers
ConsoleKey 列挙体

ConsoleKeyInfo メソッド


ConsoleKeyInfo メンバ

コンソール キーが表す文字や、Shift、Alt、Ctrl の各修飾子キーの状態など、押されたコンソール キーを記述します。

ConsoleKeyInfo データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
参照参照

関連項目

ConsoleKeyInfo 構造体
System 名前空間
ConsoleModifiers
ConsoleKey 列挙体

ConsoleKeyInfo 構造体

メモ : この構造体は、.NET Framework version 2.0 で新しく追加されたものです。

コンソール キーが表す文字や、Shift、Alt、Ctrl の各修飾子キーの状態など、押されたコンソール キーを記述します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
Public Structure ConsoleKeyInfo
Dim instance As ConsoleKeyInfo
[SerializableAttribute] 
public struct ConsoleKeyInfo
[SerializableAttribute] 
public value class ConsoleKeyInfo
/** @attribute SerializableAttribute() */ 
public final class ConsoleKeyInfo extends ValueType
JScript では、構造体を使用できますが、新規に宣言することはできません。
解説解説

ConsoleKeyInfo 型は、ユーザーが作成することを意図した型ではありません。代わりに、この型は、Console.ReadKey メソッドの呼び出しに対する応答としてユーザーに返されます。

ConsoleKeyInfo オブジェクトは、ConsoleKey 定数と、押されたコンソール キーに対応する Unicode 文字 (存在する場合) を記述します。また、ConsoleKeyInfo オブジェクトは、1 つ以上の Shift、Alt、Ctrl の各修飾子キーがコンソール キーと同時に押されたかどうかを ConsoleModifiers 値のビットごとの組み合わせで記述します。

使用例使用例

読み取り操作で ConsoleKeyInfo オブジェクトを使用するコード例を次に示します。

' This example demonstrates the Console.ReadKey() method
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      '                   0        1         2         3         4 
        5         6
      '                   123456789012345678901234567890123456879012345678901234567890
      Dim m1 As String =
 "This example discovers the console and modifier keys "
 & _
                         "that you press." & vbCrLf
      Dim m2 As String =
 "Press any combination of CTL, ALT, and SHIFT modifier keys,
 " & _
                         "and a console key." &
 vbCrLf & _
                         "Press the Escape (Esc) key to quit:
 "
      Dim m3 As String =
 "You pressed "
      Dim m4 As String =
 " (character '{0}')."
      Dim sb As 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 Then
            If(cki.Modifiers And ConsoleModifiers.Alt)
 <> 0 Then
               sb.Append("ALT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Shift)
 <> 0 Then
               sb.Append("SHIFT+")
            End If
            If(cki.Modifiers And ConsoleModifiers.Control)
 <> 0 Then
               sb.Append("CTL+")
            End If
         End If
         sb.Append(cki.Key.ToString())
         sb.AppendFormat(m4, cki.KeyChar)
         sb.AppendLine().AppendLine()
         Console.WriteLine(sb.ToString())
      Loop While cki.Key <> ConsoleKey.Escape
   End Sub 'Main ' Note: This
 example requires the Escape (Esc) key.
End Class '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 '?').
'
// 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
using namespace System;
using namespace System::Text;
int 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 = gcnew 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 ( (int)cki.Modifiers )
      {
         if ( (int)(cki.Modifiers & ConsoleModifiers::Alt)
 )
                  sb->Append( "ALT+" );

         if ( (int)(cki.Modifiers & ConsoleModifiers::Shift)
 )
                  sb->Append( "SHIFT+" );

         if ( (int)(cki.Modifiers & ConsoleModifiers::Control)
 )
                  sb->Append( "CTL+" );
      }

      sb->Append( cki.Key.ToString() );
      sb->AppendFormat( m4, cki.KeyChar );
      sb->AppendLine()->AppendLine();
      Console::WriteLine( sb );
   }
   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 '?').
*/
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ConsoleKeyInfo メンバ
System 名前空間
ConsoleModifiers
ConsoleKey 列挙体



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ConsoleKeyInfo」の関連用語

ConsoleKeyInfoのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ConsoleKeyInfoのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS