Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Console.ReadKey ()

 

Publicado: octubre de 2016

Obtiene la siguiente tecla de carácter o de función presionada por el usuario. La tecla presionada se muestra en la ventana de la consola.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

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

Valor devuelto

Type: System.ConsoleKeyInfo

Objeto que describe la constante ConsoleKey y el carácter Unicode, si existe, que corresponden a la tecla presionada en la consola. El objeto ConsoleKeyInfo también describe, en una combinación bit a bit de valores de ConsoleModifiers, si alguna de las teclas modificadoras Mayús, Alt o Ctrl se presionaron al mismo tiempo que la tecla de la consola.

Exception Condition
InvalidOperationException

La propiedad In se ha redirigido desde alguna otra secuencia distinta de la consola.

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
Disponible desde 2.0
Volver al principio
Mostrar: