1
How can I "read" which key is pressed and set an event for such a key if pressed. Example: if you press F1 Sum, F2 subtracts and so on I found this example on the C# help site, but I didn’t understand how it works:
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);
}
}
But this code just prints the keystrokes pressed
// 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 '←')
Thanks for your help
Try this: Console.Readkey(true). Key == Consolekey.F1; //this will compare whether the character typed equals F1
– Guilherme Guini
That worked.
– Guilherme HS