Console.ReadKey Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u het volgende teken of de functietoets op die door de gebruiker is ingedrukt.
Overloads
| Name | Description |
|---|---|
| ReadKey() |
Hiermee haalt u het volgende teken of de functietoets op die door de gebruiker is ingedrukt. De ingedrukt toets wordt weergegeven in het consolevenster. |
| ReadKey(Boolean) |
Hiermee haalt u het volgende teken of de functietoets op die door de gebruiker is ingedrukt. De ingedrukt-toets wordt optioneel weergegeven in het consolevenster. |
ReadKey()
Hiermee haalt u het volgende teken of de functietoets op die door de gebruiker is ingedrukt. De ingedrukt toets wordt weergegeven in het consolevenster.
public:
static ConsoleKeyInfo ReadKey();
public static ConsoleKeyInfo ReadKey();
static member ReadKey : unit -> ConsoleKeyInfo
Public Shared Function ReadKey () As ConsoleKeyInfo
Retouren
Een object dat het ConsoleKey constante en Unicode-teken beschrijft, indien aanwezig, die overeenkomen met de ingedrukt-consoletoets. Het ConsoleKeyInfo object beschrijft ook, in een bitsgewijze combinatie van ConsoleModifiers waarden, of een of meer Shift-, Alt- of Ctrl-wijzigingstoetsen tegelijkertijd met de consoletoets zijn ingedrukt.
Uitzonderingen
De In eigenschap wordt omgeleid vanuit een andere stream dan de console.
Voorbeelden
Een van de meest voorkomende toepassingen van de methode is om de ReadKey() uitvoering van het programma te stoppen totdat de gebruiker op een toets drukt en de app wordt beëindigd of een extra venster met informatie weergeeft. In het volgende voorbeeld wordt de ReadKey() methode gebruikt om te wachten totdat de gebruiker op Enter drukt voordat de app wordt beëindigd.
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
Houd er rekening mee dat deze overbelasting van de ReadKey methode standaard alle weer te geven toetsen weergeeft die de gebruiker op de console drukt. Als u ze wilt onderdrukken, roept u de ReadKey methode aan met een intercept argument van true.
In het volgende voorbeeld wordt de ReadKey() methode gebruikt om informatie weer te geven over welke sleutel de gebruiker heeft ingedrukt.
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
Opmerkingen
De ReadKey methode wacht, dat wil gezegd, blokkeert op de thread die de ReadKey methode uitgeeft, totdat een teken- of functietoets wordt ingedrukt. Een teken- of functietoets kan worden ingedrukt in combinatie met een of meer Alt-, Ctrl- of Shift-wijzigingstoetsen. Als u echter zelf op een wijzigingstoets drukt, wordt de ReadKey methode niet geretourneerd.
Afhankelijk van uw toepassing kunt u de ReadKey methode gebruiken in combinatie met de KeyAvailable eigenschap.
De ReadKey methode leest van het toetsenbord, zelfs als de standaardinvoer wordt omgeleid naar een bestand met de SetIn methode.
Zie ook
Van toepassing op
ReadKey(Boolean)
Hiermee haalt u het volgende teken of de functietoets op die door de gebruiker is ingedrukt. De ingedrukt-toets wordt optioneel weergegeven in het consolevenster.
public:
static ConsoleKeyInfo ReadKey(bool intercept);
public static ConsoleKeyInfo ReadKey(bool intercept);
static member ReadKey : bool -> ConsoleKeyInfo
Public Shared Function ReadKey (intercept As Boolean) As ConsoleKeyInfo
Parameters
- intercept
- Boolean
Bepaalt of de ingedrukt toets in het consolevenster moet worden weergegeven.
true om de ingedrukt toets niet weer te geven; anders, false.
Retouren
Een object dat het ConsoleKey constante en Unicode-teken beschrijft, indien aanwezig, die overeenkomen met de ingedrukt-consoletoets. Het ConsoleKeyInfo object beschrijft ook, in een bitsgewijze combinatie van ConsoleModifiers waarden, of een of meer Shift-, Alt- of Ctrl-wijzigingstoetsen tegelijkertijd met de consoletoets zijn ingedrukt.
Uitzonderingen
De In eigenschap wordt omgeleid vanuit een andere stream dan de console.
Voorbeelden
Een van de meest voorkomende toepassingen van de methode is om de ReadKey uitvoering van het programma te stoppen totdat de gebruiker op een toets drukt en de app wordt beëindigd of een extra venster met informatie weergeeft. In het volgende voorbeeld wordt de ReadKey(Boolean) methode gebruikt om te wachten totdat de gebruiker op Enter drukt voordat de app wordt beëindigd. Houd er rekening mee dat als de gebruiker op een andere toets drukt, deze niet wordt herhaald naar de console.
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
In het volgende voorbeeld wordt de ReadKey(Boolean) methode gebruikt om informatie weer te geven over de toets die door een gebruiker is ingedrukt zonder die sleutel naar de console weer te geven.
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 '←')
Opmerkingen
De ReadKey methode wacht, dat wil gezegd, blokkeert op de thread die de ReadKey methode uitgeeft, totdat een teken- of functietoets wordt ingedrukt. Een teken- of functietoets kan worden ingedrukt in combinatie met een of meer Alt-, Ctrl- of Shift-wijzigingstoetsen. Als u echter zelf op een wijzigingstoets drukt, wordt de ReadKey methode niet geretourneerd.
Als de intercept parameter is true, wordt de ingedrukt toets onderschept en niet weergegeven in het consolevenster. Anders wordt de ingedrukt toets weergegeven.
Afhankelijk van uw toepassing kunt u de ReadKey methode gebruiken in combinatie met de KeyAvailable eigenschap.
De ReadKey methode leest van het toetsenbord, zelfs als de standaardinvoer wordt omgeleid naar een bestand met de SetIn methode.