0
How to assign string value to a certain key pressed by the user? (C# only)
What code can I use to assign a value to a key (a, b, c, 1, 2 and so on) pressed by the user on a console application? Example, if the user presses A, the console needs to say Ashley. Or whatever. Please help, I am a total beginner and don't know any coders!?! If you want to e-mail, send a message to judithkok52@gmail.com Thanks guys!!
2 Respuestas
+ 11
You can retrieve a keystroke with Console.ReadKey(), and store it in the data type ConsoleKey.
To use it, do this:
ConsoleKey input;
Console.WriteLine("Press any key:);
input = Console.ReadKey().Key;
.Key allows the keystroke to be stored as ConsoleKey. Then, use a switch to check its value:
switch (input)
{
case ConsoleKey.A:
Console.WriteLine("Ashley");
break;
//...
}
+ 1
Thank you ♡