Console.ReadKey Console.ReadKey Console.ReadKey Console.ReadKey Method

Definizione

Ottiene il carattere successivo o il tasto funzione premuto dall'utente.Obtains the next character or function key pressed by the user.

Overload

ReadKey() ReadKey() ReadKey() ReadKey()

Ottiene il carattere successivo o il tasto funzione premuto dall'utente.Obtains the next character or function key pressed by the user. Il tasto premuto viene visualizzato nella finestra della console.The pressed key is displayed in the console window.

ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean)

Ottiene il carattere successivo o il tasto funzione premuto dall'utente.Obtains the next character or function key pressed by the user. Il tasto premuto viene visualizzato facoltativamente nella finestra della console.The pressed key is optionally displayed in the console window.

ReadKey() ReadKey() ReadKey() ReadKey()

Ottiene il carattere successivo o il tasto funzione premuto dall'utente.Obtains the next character or function key pressed by the user. Il tasto premuto viene visualizzato nella finestra della console.The pressed key is displayed in the console window.

public:
 static ConsoleKeyInfo ReadKey();
public static ConsoleKeyInfo ReadKey ();
static member ReadKey : unit -> ConsoleKeyInfo
Public Shared Function ReadKey () As ConsoleKeyInfo

Restituisce

Oggetto che descrive la costante ConsoleKey e il carattere Unicode, se presente, che corrispondono al tasto premuto sulla console.An object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. L'oggetto ConsoleKeyInfo descrive inoltre, in una combinazione bit per bit di valori ConsoleModifiers, se sono stati premuti contemporaneamente al tasto della console uno o più tasti di modifica MAIUSC, ALT o CTRL.The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

Eccezioni

La proprietà In viene reindirizzata da un flusso diverso da quello della console.The In property is redirected from some stream other than the console.

Esempi

Uno dei più comuni Usa del ReadKey() metodo consiste nell'interrompere l'esecuzione del programma fino a quando l'utente preme un tasto mentre l'app termina o viene visualizzato un'ulteriore finestra di informazioni.One of the most common uses of the ReadKey() method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information. L'esempio seguente usa il ReadKey() metodo per attendere che l'utente prema INVIO prima di terminare l'app.The following example uses the ReadKey() method to wait for the user to press the Enter key before terminating the app.

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
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

Si noti che questo overload del metodo di ReadKey metodo per impostazione predefinita restituisce le chiavi visualizzabile che l'utente preme nella console.Note that this overload of the ReadKey method by default echoes any displayable keys that the user presses to the console. Per eliminarli, chiamare il ReadKey metodo con un intercept argomento di true.To suppress them, call the ReadKey method with an intercept argument of true.

L'esempio seguente usa il ReadKey() metodo per visualizzare le informazioni sulla quale tasto premuto dall'utente.The following example uses the ReadKey() method to display information about which key the user pressed.

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 
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 

Commenti

Il ReadKey metodo attende, vale a dire blocchi sul thread di esecuzione di ReadKey (metodo), fino a quando non viene premuto un tasto carattere o una funzione.The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. Può essere premuto un tasto carattere o una funzione in combinazione con uno o più tasti di modifica Alt, Ctrl o MAIUSC.A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. Tuttavia, premere un tasto di modifica da sola non genererà il ReadKey metodo da restituire.However, pressing a modifier key by itself will not cause the ReadKey method to return.

A seconda dell'applicazione, si potrebbe voler usare il ReadKey metodo in combinazione con il KeyAvailable proprietà.Depending on your application, you might want to use the ReadKey method in conjunction with the KeyAvailable property.

Il ReadKey metodo legge da tastiera, anche se l'input standard viene reindirizzato a un file con il SetIn (metodo).The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

Vedi anche

ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean) ReadKey(Boolean)

Ottiene il carattere successivo o il tasto funzione premuto dall'utente.Obtains the next character or function key pressed by the user. Il tasto premuto viene visualizzato facoltativamente nella finestra della console.The pressed key is optionally displayed in the console window.

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

Parametri

intercept
Boolean Boolean Boolean Boolean

Determina se visualizzare il tasto premuto nella finestra della console.Determines whether to display the pressed key in the console window. true per non visualizzare il tasto premuto; in caso contrario, false.true to not display the pressed key; otherwise, false.

Restituisce

Oggetto che descrive la costante ConsoleKey e il carattere Unicode, se presente, che corrispondono al tasto premuto sulla console.An object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. L'oggetto ConsoleKeyInfo descrive inoltre, in una combinazione bit per bit di valori ConsoleModifiers, se sono stati premuti contemporaneamente al tasto della console uno o più tasti di modifica MAIUSC, ALT o CTRL.The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

Eccezioni

La proprietà In viene reindirizzata da un flusso diverso da quello della console.The In property is redirected from some stream other than the console.

Esempi

Uno dei più comuni Usa del ReadKey metodo consiste nell'interrompere l'esecuzione del programma fino a quando l'utente preme un tasto mentre l'app termina o viene visualizzato un'ulteriore finestra di informazioni.One of the most common uses of the ReadKey method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information. L'esempio seguente usa il ReadKey(Boolean) metodo per attendere che l'utente prema INVIO prima di terminare l'app.The following example uses the ReadKey(Boolean) method to wait for the user to press the Enter key before terminating the app. Si noti che, se l'utente preme un tasto qualsiasi, non è restituito alla console.Note that, if the user presses any other key, it is not echoed to the 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
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

L'esempio seguente usa il ReadKey(Boolean) metodo per visualizzare informazioni relative al tasto premuto dall'utente senza la restituzione di tale chiave nella console.The following example uses the ReadKey(Boolean) method to display information about the key pressed by a user without echoing that key to the console.

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 '←')
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 '←')

Commenti

Il ReadKey metodo attende, vale a dire blocchi sul thread di esecuzione di ReadKey (metodo), fino a quando non viene premuto un tasto carattere o una funzione.The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. Può essere premuto un tasto carattere o una funzione in combinazione con uno o più tasti di modifica Alt, Ctrl o MAIUSC.A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. Tuttavia, premere un tasto di modifica da sola non genererà il ReadKey metodo da restituire.However, pressing a modifier key by itself will not cause the ReadKey method to return.

Se il intercept parametro è true, il tasto premuto viene intercettato e non visualizzati nella finestra della console; in caso contrario, il tasto premuto viene visualizzato.If the intercept parameter is true, the pressed key is intercepted and not displayed in the console window; otherwise, the pressed key is displayed.

A seconda dell'applicazione, si potrebbe voler usare il ReadKey metodo in combinazione con il KeyAvailable proprietà.Depending on your application, you might want to use the ReadKey method in conjunction with the KeyAvailable property.

Il ReadKey metodo legge da tastiera, anche se l'input standard viene reindirizzato a un file con il SetIn (metodo).The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

Vedi anche

Si applica a