Console.ReadKey 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取用户按下的下一个字符或功能键。
重载
ReadKey() |
获取用户按下的下一个字符或功能键。 按下的键显示在控制台窗口中。 |
ReadKey(Boolean) |
获取用户按下的下一个字符或功能键。 按下的键可以选择显示在控制台窗口中。 |
ReadKey()
获取用户按下的下一个字符或功能键。 按下的键显示在控制台窗口中。
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 值的按位组合中描述是否在按下控制台键的同时按下了一个或多个 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 方法重载会回显用户在控制台上按下的所有可显示键。 若要取消它们,请 ReadKey 使用参数调用方法 intercept
true
。
下面的示例使用 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 方法返回。
根据你的应用程序,你可能想要将 ReadKey 方法与属性结合使用 KeyAvailable 。
ReadKey即使使用方法将标准输入重定向到文件,该方法也会从键盘读取 SetIn 。
另请参阅
适用于
ReadKey(Boolean)
获取用户按下的下一个字符或功能键。 按下的键可以选择显示在控制台窗口中。
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 值的按位组合中描述是否在按下控制台键的同时按下了一个或多个 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
,则会截获按下的键; 否则,会显示按下的键。
根据你的应用程序,你可能想要将 ReadKey 方法与属性结合使用 KeyAvailable 。
ReadKey即使使用方法将标准输入重定向到文件,该方法也会从键盘读取 SetIn 。