How to assign string value to a certain key pressed by the user? (C# only) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!!

19th Mar 2017, 5:54 PM
Judith Joubert
Judith Joubert - avatar
2 Answers
+ 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; //... }
19th Mar 2017, 6:56 PM
Tamra
Tamra - avatar
+ 1
Thank you ♡
22nd Mar 2017, 1:39 PM
Judith Joubert
Judith Joubert - avatar