Console.ReadKey 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得使用者按下的下一個字元或功能鍵。
多載
ReadKey() |
取得使用者按下的下一個字元或功能鍵。 按下的按鍵會顯示在主控台視窗中。 |
ReadKey(Boolean) |
取得使用者按下的下一個字元或功能鍵。 按下的按鍵會選擇性地顯示在主控台視窗中。 |
ReadKey()
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
取得使用者按下的下一個字元或功能鍵。 按下的按鍵會顯示在主控台視窗中。
public:
static ConsoleKeyInfo ReadKey();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleKeyInfo ReadKey ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleKeyInfo ReadKey ();
public static ConsoleKeyInfo ReadKey ();
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member ReadKey : unit -> ConsoleKeyInfo
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member ReadKey : unit -> ConsoleKeyInfo
static member ReadKey : unit -> ConsoleKeyInfo
Public Shared Function ReadKey () As ConsoleKeyInfo
傳回
物件,可描述對應到所按主控台按鍵的 ConsoleKey 常數和 Unicode 字元 (如有此項)。 ConsoleKeyInfo 物件也描述在 ConsoleModifiers 值的位元 (Bitwise) 組合中,是否有一或多個 Shift、Alt 或 Ctrl 輔助按鍵與主控台按鍵同時按下。
- 屬性
例外狀況
會將 In 屬性從主控台之外的某些資料流重新導向。
範例
方法最常見的用法 ReadKey() 之一是停止程式執行,直到使用者按下按鍵並應用程式終止或顯示其他資訊視窗為止。 下列範例會 ReadKey() 使用 方法來等候使用者先按下 Enter 鍵,再終止應用程式。
using System;
public class Example
{
public static void Main()
{
DateTime dat = DateTime.Now;
Console.WriteLine("The time: {0:d} at {0:t}", dat);
TimeZoneInfo tz = TimeZoneInfo.Local;
Console.WriteLine("The time zone: {0}\n",
tz.IsDaylightSavingTime(dat) ?
tz.DaylightName : tz.StandardName);
Console.Write("Press <Enter> to exit... ");
while (Console.ReadKey().Key != ConsoleKey.Enter) {}
}
}
// The example displays output like the following:
// The time: 11/11/2015 at 4:02 PM:
// The time zone: Pacific Standard Time
open System
let dat = DateTime.Now
printfn $"The time: {dat:d} at {dat:t}"
let tz = TimeZoneInfo.Local
printfn $"The time zone: {if tz.IsDaylightSavingTime dat then tz.DaylightName else tz.StandardName}\n"
printf"Press <Enter> to exit... "
while Console.ReadKey().Key <> ConsoleKey.Enter do ()
// The example displays output like the following:
// The time: 12/28/2021 at 8:35 PM
// The time zone: Pacific Standard Time
Module Example
Public Sub Main()
Dim dat As Date = Date.Now
Console.WriteLine("The time: {0:d} at {0:t}", dat)
Dim tz As TimeZoneInfo = TimeZoneInfo.Local
Console.WriteLine("The time zone: {0}",
If(tz.IsDaylightSavingTime(dat),
tz.DaylightName, tz.StandardName))
Console.WriteLine()
Console.Write("Press <Enter> to exit... ")
Do While Console.ReadKey().Key <> ConsoleKey.Enter
Loop
End Sub
End Module
' The example displays the following output:
' The time: 11/11/2015 at 4:02 PM
' The time zone: Pacific Standard Time
請注意,此方法的 ReadKey 這個多載預設會回應使用者按下主控台的任何可顯示按鍵。 若要隱藏它們,請使用 intercept
的 true
引數呼叫 ReadKey 方法。
下列範例會 ReadKey() 使用 方法來顯示使用者按下哪一個按鍵的相關資訊。
using namespace System;
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) != ConsoleModifiers()) Console::Write("ALT+");
if((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
if((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) 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 }
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
open System
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput <- true
printfn "Press any combination of CTL, ALT, and SHIFT, and a console key."
printfn "Press the Escape (Esc) key to quit: \n"
let mutable cki = Unchecked.defaultof<ConsoleKeyInfo>
while cki.Key <> ConsoleKey.Escape do
cki <- Console.ReadKey()
printf " --- You pressed "
if int (cki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then printf "ALT+"
if int (cki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then printf "SHIFT+"
if int (cki.Modifiers &&& ConsoleModifiers.Control) <> 0 then printf "CTL+"
printfn $"{cki.Key}"
// 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
Class Example
Public Shared Sub Main()
Dim cki As ConsoleKeyInfo
' 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: " + vbCrLf)
Do
cki = Console.ReadKey()
Console.Write(" --- You pressed ")
If (cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then Console.Write("ALT+")
If (cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then Console.Write("SHIFT+")
If (cki.Modifiers And ConsoleModifiers.Control) <> 0 Then Console.Write("CTL+")
Console.WriteLine(cki.Key.ToString)
Loop While cki.Key <> ConsoleKey.Escape
End Sub
End Class
' 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
備註
方法 ReadKey 會等候,也就是在發出 ReadKey 方法的執行緒上封鎖,直到按下字元或函式按鍵為止。 字元或函式按鍵可以與一或多個 Alt、Ctrl 或 Shift 修飾詞鍵結合來按下。 不過,按修飾詞鍵本身並不會讓 ReadKey 方法傳回。
視您的應用程式而定,您可能想要搭配 KeyAvailable 屬性使用 ReadKey 方法。
即使使用 方法將標準輸入重新導向至檔案 SetIn ,方法 ReadKey 仍會從鍵盤讀取。
另請參閱
適用於
ReadKey(Boolean)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
取得使用者按下的下一個字元或功能鍵。 按下的按鍵會選擇性地顯示在主控台視窗中。
public:
static ConsoleKeyInfo ReadKey(bool intercept);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleKeyInfo ReadKey (bool intercept);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleKeyInfo ReadKey (bool intercept);
public static ConsoleKeyInfo ReadKey (bool intercept);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member ReadKey : bool -> ConsoleKeyInfo
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member ReadKey : bool -> ConsoleKeyInfo
static member ReadKey : bool -> ConsoleKeyInfo
Public Shared Function ReadKey (intercept As Boolean) As ConsoleKeyInfo
參數
- intercept
- Boolean
決定是否要在主控台視窗中顯示按下的按鍵。
true
表示不顯示按下的按鍵;否則為 false
。
傳回
物件,可描述對應到所按主控台按鍵的 ConsoleKey 常數和 Unicode 字元 (如有此項)。 ConsoleKeyInfo 物件也描述在 ConsoleModifiers 值的位元 (Bitwise) 組合中,是否有一或多個 Shift、Alt 或 Ctrl 輔助按鍵與主控台按鍵同時按下。
- 屬性
例外狀況
會將 In 屬性從主控台之外的某些資料流重新導向。
範例
方法最常見的用法 ReadKey 之一是停止程式執行,直到使用者按下按鍵並應用程式終止或顯示其他資訊視窗為止。 下列範例會 ReadKey(Boolean) 使用 方法來等候使用者先按下 Enter 鍵,再終止應用程式。 請注意,如果使用者按下任何其他按鍵,就不會回應主控台。
using System;
public class Example
{
public static void Main()
{
DateTime dat = DateTime.Now;
Console.WriteLine("The time: {0:d} at {0:t}", dat);
TimeZoneInfo tz = TimeZoneInfo.Local;
Console.WriteLine("The time zone: {0}\n",
tz.IsDaylightSavingTime(dat) ?
tz.DaylightName : tz.StandardName);
Console.Write("Press <Enter> to exit... ");
while (Console.ReadKey(true).Key != ConsoleKey.Enter) {}
}
}
// The example displays output like the following:
// The time: 11/11/2015 at 4:02 PM:
// The time zone: Pacific Standard Time
open System
let dat = DateTime.Now
printfn $"The time: {dat:d} at {dat:t}"
let tz = TimeZoneInfo.Local
printfn $"The time zone: {if tz.IsDaylightSavingTime dat then tz.DaylightName else tz.StandardName}\n"
printf"Press <Enter> to exit... "
while Console.ReadKey(true).Key <> ConsoleKey.Enter do ()
// The example displays output like the following:
// The time: 12/28/2021 at 8:37 PM
// The time zone: Pacific Standard Time
Module Example
Public Sub Main()
Dim dat As Date = Date.Now
Console.WriteLine("The time: {0:d} at {0:t}", dat)
Dim tz As TimeZoneInfo = TimeZoneInfo.Local
Console.WriteLine("The time zone: {0}",
If(tz.IsDaylightSavingTime(dat),
tz.DaylightName, tz.StandardName))
Console.WriteLine()
Console.Write("Press <Enter> to exit... ")
Do While Console.ReadKey(True).Key <> ConsoleKey.Enter
Loop
End Sub
End Module
' The example displays the following output:
' The time: 11/11/2015 at 4:02 PM
' The time zone: Pacific Standard Time
下列範例會 ReadKey(Boolean) 使用 方法來顯示使用者按下的按鍵相關資訊,而不會將該按鍵回應至主控台。
using namespace System;
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(true);
Console::Write("You pressed ");
if ((cki.Modifiers & ConsoleModifiers::Alt) != ConsoleModifiers()) Console::Write("ALT+");
if ((cki.Modifiers & ConsoleModifiers::Shift) != ConsoleModifiers()) Console::Write("SHIFT+");
if ((cki.Modifiers & ConsoleModifiers::Control) != ConsoleModifiers()) Console::Write("CTL+");
Console::WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
} 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:
//
// You pressed CTL+A (character '☺')
// You pressed C (character 'c')
// You pressed CTL+C (character '♥')
// You pressed K (character 'k')
// You pressed ALT+I (character 'i')
// You pressed ALT+U (character 'u')
// You pressed ALT+SHIFT+H (character 'H')
// You pressed Escape (character '←')
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(true);
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("{0} (character '{1}')", cki.Key, cki.KeyChar);
} 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:
//
// You pressed CTL+A (character '☺')
// You pressed C (character 'c')
// You pressed CTL+C (character '♥')
// You pressed K (character 'k')
// You pressed ALT+I (character 'i')
// You pressed ALT+U (character 'u')
// You pressed ALT+SHIFT+H (character 'H')
// You pressed Escape (character '←')
open System
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput <- true
printfn "Press any combination of CTL, ALT, and SHIFT, and a console key."
printfn "Press the Escape (Esc) key to quit: \n"
let mutable cki = Unchecked.defaultof<ConsoleKeyInfo>
while cki.Key <> ConsoleKey.Escape do
cki <- Console.ReadKey true
printf "You pressed "
if int (cki.Modifiers &&& ConsoleModifiers.Alt) <> 0 then printf "ALT+"
if int (cki.Modifiers &&& ConsoleModifiers.Shift) <> 0 then printf "SHIFT+"
if int (cki.Modifiers &&& ConsoleModifiers.Control) <> 0 then printf "CTL+"
printfn $"{cki.Key} (character '{cki.KeyChar}')"
// 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:
//
// You pressed CTL+A (character '☺')
// You pressed C (character 'c')
// You pressed CTL+C (character '♥')
// You pressed K (character 'k')
// You pressed ALT+I (character 'i')
// You pressed ALT+U (character 'u')
// You pressed ALT+SHIFT+H (character 'H')
// You pressed Escape (character '←')
Class Example
Public Shared Sub Main()
Dim cki As ConsoleKeyInfo
' 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: " + vbCrLf)
Do
cki = Console.ReadKey(True)
Console.Write("You pressed ")
If (cki.Modifiers And ConsoleModifiers.Alt) <> 0 Then Console.Write("ALT+")
If (cki.Modifiers And ConsoleModifiers.Shift) <> 0 Then Console.Write("SHIFT+")
If (cki.Modifiers And ConsoleModifiers.Control) <> 0 Then Console.Write("CTL+")
Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar)
Loop While cki.Key <> ConsoleKey.Escape
End Sub
End Class
' 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:
'
' You pressed CTL+A (character '☺')
' You pressed C (character 'c')
' You pressed CTL+C (character '♥')
' You pressed K (character 'k')
' You pressed ALT+I (character 'i')
' You pressed ALT+U (character 'u')
' You pressed ALT+SHIFT+H (character 'H')
' You pressed Escape (character '←')
備註
方法 ReadKey 會等候,也就是在發出 ReadKey 方法的執行緒上封鎖,直到按下字元或函式按鍵為止。 字元或函式按鍵可以與一或多個 Alt、Ctrl 或 Shift 修飾詞鍵結合來按下。 不過,按修飾詞鍵本身並不會讓 ReadKey 方法傳回。
intercept
如果 參數為 true
,則會攔截已按下的按鍵,而不會顯示在主控台視窗中,否則會顯示已按下的按鍵。
視您的應用程式而定,您可能想要搭配 KeyAvailable 屬性使用 ReadKey 方法。
即使使用 方法將標準輸入重新導向至檔案 SetIn ,方法 ReadKey 仍會從鍵盤讀取。