匯出 (0) 列印
全部展開

ConsoleKeyInfo 結構

更新:2007 年 11 月

說明按下的主控台按鍵,包括主控台按鍵表示的字元,以及 SHIFT、ALT 和 CTRL 輔助按鍵 (Modifier Key) 的狀態。

命名空間:  System
組件:  mscorlib (在 mscorlib.dll 中)

[SerializableAttribute]
public struct ConsoleKeyInfo
/** @attribute SerializableAttribute */ 
public final class ConsoleKeyInfo extends ValueType
JScript 支援使用結構,但不支援新結構的宣告。

使用者無法建立 ConsoleKeyInfo 型別。相反地,使用者可使用它來回應 Console.ReadKey 方法呼叫。

ConsoleKeyInfo 物件描述按下主控台按鍵所對應的 ConsoleKey 常數和 Unicode 字元 (如果有的話)。ConsoleKeyInfo 物件也描述在 ConsoleModifiers 值的位元 (Bitwise) 組合中,是否有一或多個 SHIFT、ALT 或 CTRL 輔助按鍵與主控台按鍵同時按下。

下列程式碼範例示範在讀取作業中使用 ConsoleKeyInfo 物件。

// 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 '?').
*/


這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

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

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

.NET Framework

支援版本:3.5、3.0、2.0

社群新增項目

新增
顯示:
© 2015 Microsoft