Maha

Maha

  • NA
  • 0
  • 324.8k

Console.ReadKey

Jul 5 2013 3:22 PM
This program is given in following website.
http://www.dotnetperls.com/console-readkey

This new code (info.KeyChar == 'X') instead of (info.Key == ConsoleKey.X) is giving different last line output. Please explain the reason. Code is highlighted.

using System;
class Program
{
static void Main()
{
Console.WriteLine("... Press escape, a, then control X");
// Call ReadKey method and store result in local variable.
// ... Then test the result for escape.

ConsoleKeyInfo info = Console.ReadKey();

if (info.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed escape!");
}

// Call ReadKey again and test for the letter a.
info = Console.ReadKey();

if (info.KeyChar == 'a')
{
Console.WriteLine("You pressed a");
}

// Call ReadKey again and test for control-X.
// ... This implements a shortcut sequence.
info = Console.ReadKey();

if (info.Key == ConsoleKey.X && info.Modifiers == ConsoleModifiers.Control)
{
Console.WriteLine("You pressed control X");
}
Console.Read();
}
}
/*
... Press escape, a, then control X
?You pressed escape!
aYou pressed a
?You pressed control X
*/


Answers (2)