Csharp/CSharp Tutorial/Development/ConsoleKey — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Read keystrokes from the console by using ReadKey()
using System;
class MainClass {
public static void Main() {
ConsoleKeyInfo keypress;
Console.WriteLine("Enter keystrokes. Enter Q to stop.");
do {
keypress = Console.ReadKey(); // read keystrokes
Console.WriteLine(" Your key is: " + keypress.KeyChar);
// Check for modifier keys.
if((ConsoleModifiers.Alt & keypress.Modifiers) != 0)
Console.WriteLine("Alt key pressed.");
if((ConsoleModifiers.Control & keypress.Modifiers) != 0)
Console.WriteLine("Control key pressed.");
if((ConsoleModifiers.Shift & keypress.Modifiers) != 0)
Console.WriteLine("Shift key pressed.");
} while(keypress.KeyChar != "Q");
}
}
Enter keystrokes. Enter Q to stop. q Your key is: q w Your key is: w e Your key is: e q Your key is: q w Your key is: w e Your key is: e r Your key is: r e Your key is: e w Your key is: w q Your key is: q Q Your key is: Q Shift key pressed.
Use ConsoleKey to get input
using System;
using System.Collections.Generic;
class MainClass
{
public static void Main()
{
ConsoleKeyInfo key;
Console.WriteLine("Process input until the user enters Alt-X or Alt-x");
do
{
key = Console.ReadKey(true);
Console.WriteLine(key);
if (key.Key == ConsoleKey.F1)
{
Console.WriteLine("F1");
}
// Handle backspace.
if (key.Key == ConsoleKey.Backspace)
{
Console.WriteLine("Backspace");
}
// Handle Escape.
else if (key.Key == ConsoleKey.Escape)
{
Console.WriteLine("Escape");
}
// Handle character input.
else if (key.Key >= ConsoleKey.A && key.Key <= ConsoleKey.Z)
{
Console.WriteLine(">=ConsoleKey.A && <= ConsoleKey.Z");
}
} while (key.Key != ConsoleKey.X || key.Modifiers != ConsoleModifiers.Alt);
}
}
Process input until the user enters Alt-X or Alt-x System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z System.ConsoleKeyInfo >=ConsoleKey.A && <= ConsoleKey.Z