Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Console.ReadKey ()

 

Data di pubblicazione: ottobre 2016

Ottiene il carattere successivo o il tasto funzione premuto dall'utente. Il tasto premuto viene visualizzato nella finestra della console.

Spazio dei nomi:   System
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static ConsoleKeyInfo ReadKey()

Valore restituito

Type: System.ConsoleKeyInfo

Oggetto che descrive la costante ConsoleKey e il carattere Unicode, se presente, che corrispondono al tasto premuto sulla console. 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.

Exception Condition
InvalidOperationException

La proprietà In viene reindirizzata da un flusso diverso da quello della console.

The M:System.Console.ReadKey method waits, that is, blocks on the thread issuing the M:System.Console.ReadKey method, until a character or function key is pressed. A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. However, pressing a modifier key by itself will not cause the M:System.Console.ReadKey method to return.

Depending on your application, you might want to use the M:System.Console.ReadKey method in conjunction with the P:System.Console.KeyAvailable property.

The M:System.Console.ReadKey method reads from the keyboard even if the standard input is redirected to a file with the M:System.Console.SetIn(System.IO.TextReader) method.

One of the most common uses of the M:System.Console.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. The following example uses the M:System.Console.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

Note that this overload of the Overload:System.Console.ReadKey method by default echoes any displayable keys that the user presses to the console. To suppress them, call the M:System.Console.ReadKey(System.Boolean) method with an intercept argument of true.

The following example uses the M:System.Console.ReadKey method to display information about which key the user pressed..

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 

.NET Framework
Disponibile da 2.0
Torna all'inizio
Mostra: