Console.ReadKey Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft das nächste Zeichen oder die nächste Funktionstaste ab, die vom Benutzer gedrückt wird.
Überlädt
| Name | Beschreibung |
|---|---|
| ReadKey() |
Ruft das nächste Zeichen oder die nächste Funktionstaste ab, die vom Benutzer gedrückt wird. Die gedrückte Taste wird im Konsolenfenster angezeigt. |
| ReadKey(Boolean) |
Ruft das nächste Zeichen oder die nächste Funktionstaste ab, die vom Benutzer gedrückt wird. Die gedrückte Taste wird optional im Konsolenfenster angezeigt. |
ReadKey()
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
Ruft das nächste Zeichen oder die nächste Funktionstaste ab, die vom Benutzer gedrückt wird. Die gedrückte Taste wird im Konsolenfenster angezeigt.
public:
static ConsoleKeyInfo ReadKey();
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleKeyInfo ReadKey();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleKeyInfo ReadKey();
public static ConsoleKeyInfo ReadKey();
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member ReadKey : unit -> ConsoleKeyInfo
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member ReadKey : unit -> ConsoleKeyInfo
static member ReadKey : unit -> ConsoleKeyInfo
Public Shared Function ReadKey () As ConsoleKeyInfo
Gibt zurück
Ein Objekt, das ggf. das ConsoleKey Konstante- und Unicode-Zeichen beschreibt, das der gedrückten Konsolentaste entspricht. Das ConsoleKeyInfo Objekt beschreibt auch in einer bitweisen Kombination von ConsoleModifiers Werten, ob eine oder mehrere UMSCHALT-, ALT- oder STRG-Modifizierertasten gleichzeitig mit der Konsolentaste gedrückt wurden.
- Attribute
Ausnahmen
Die In Eigenschaft wird von einem anderen Datenstrom als der Konsole umgeleitet.
Beispiele
Eine der häufigsten Verwendungsmöglichkeiten der ReadKey() Methode besteht darin, die Programmausführung anzuhalten, bis der Benutzer eine Taste drückt und die App entweder beendet oder ein zusätzliches Informationsfenster anzeigt. Im folgenden Beispiel wird die ReadKey() Methode verwendet, um zu warten, bis der Benutzer die EINGABETASTE drückt, bevor die App beendet wird.
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
Beachten Sie, dass diese Überladung der ReadKey Methode standardmäßig alle angezeigten Tasten wiedergibt, die der Benutzer auf die Konsole drückt. Um sie zu unterdrücken, rufen Sie die ReadKey Methode mit einem intercept Argument von true.
Im folgenden Beispiel wird die ReadKey() Methode verwendet, um Informationen darüber anzuzeigen, welche Taste der Benutzer gedrückt hat.
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
Hinweise
Die ReadKey Methode wartet, d. h. Blöcke auf dem Thread, der die ReadKey Methode ausgibt, bis ein Zeichen oder eine Funktionstaste gedrückt wird. Ein Zeichen oder eine Funktionstaste kann in Kombination mit einer oder mehreren ALT-, STRG- oder UMSCHALTTASTEn gedrückt werden. Das Drücken einer Zusatztaste allein führt jedoch nicht dazu, dass die ReadKey Methode zurückgegeben wird.
Je nach Anwendung sollten Sie die ReadKey Methode in Verbindung mit der KeyAvailable Eigenschaft verwenden.
Die ReadKey Methode liest von der Tastatur aus, auch wenn die Standardeingabe mit der SetIn Methode an eine Datei umgeleitet wird.
Weitere Informationen
Gilt für:
ReadKey(Boolean)
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
- Quelle:
- Console.cs
Ruft das nächste Zeichen oder die nächste Funktionstaste ab, die vom Benutzer gedrückt wird. Die gedrückte Taste wird optional im Konsolenfenster angezeigt.
public:
static ConsoleKeyInfo ReadKey(bool intercept);
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleKeyInfo ReadKey(bool intercept);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleKeyInfo ReadKey(bool intercept);
public static ConsoleKeyInfo ReadKey(bool intercept);
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member ReadKey : bool -> ConsoleKeyInfo
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member ReadKey : bool -> ConsoleKeyInfo
static member ReadKey : bool -> ConsoleKeyInfo
Public Shared Function ReadKey (intercept As Boolean) As ConsoleKeyInfo
Parameter
- intercept
- Boolean
Bestimmt, ob die gedrückte Taste im Konsolenfenster angezeigt werden soll.
truedie gedrückte Taste nicht anzuzeigen; andernfalls . false
Gibt zurück
Ein Objekt, das ggf. das ConsoleKey Konstante- und Unicode-Zeichen beschreibt, das der gedrückten Konsolentaste entspricht. Das ConsoleKeyInfo Objekt beschreibt auch in einer bitweisen Kombination von ConsoleModifiers Werten, ob eine oder mehrere UMSCHALT-, ALT- oder STRG-Modifizierertasten gleichzeitig mit der Konsolentaste gedrückt wurden.
- Attribute
Ausnahmen
Die In Eigenschaft wird von einem anderen Datenstrom als der Konsole umgeleitet.
Beispiele
Eine der häufigsten Verwendungsmöglichkeiten der ReadKey Methode besteht darin, die Programmausführung anzuhalten, bis der Benutzer eine Taste drückt und die App entweder beendet oder ein zusätzliches Informationsfenster anzeigt. Im folgenden Beispiel wird die ReadKey(Boolean) Methode verwendet, um zu warten, bis der Benutzer die EINGABETASTE drückt, bevor die App beendet wird. Beachten Sie, dass die Konsole nicht wiedergegeben wird, wenn der Benutzer eine andere Taste drückt.
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
Im folgenden Beispiel wird die ReadKey(Boolean) Methode verwendet, um Informationen über die taste anzuzeigen, die von einem Benutzer gedrückt wird, ohne dass diese Taste an die Konsole wiedergegeben wird.
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 '←')
Hinweise
Die ReadKey Methode wartet, d. h. Blöcke auf dem Thread, der die ReadKey Methode ausgibt, bis ein Zeichen oder eine Funktionstaste gedrückt wird. Ein Zeichen oder eine Funktionstaste kann in Kombination mit einer oder mehreren ALT-, STRG- oder UMSCHALTTASTEn gedrückt werden. Das Drücken einer Zusatztaste allein führt jedoch nicht dazu, dass die ReadKey Methode zurückgegeben wird.
Wenn der intercept Parameter lautet true, wird die gedrückte Taste abgefangen und nicht im Konsolenfenster angezeigt. Andernfalls wird die gedrückte Taste angezeigt.
Je nach Anwendung sollten Sie die ReadKey Methode in Verbindung mit der KeyAvailable Eigenschaft verwenden.
Die ReadKey Methode liest von der Tastatur aus, auch wenn die Standardeingabe mit der SetIn Methode an eine Datei umgeleitet wird.