Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConsoleKeyInfo, the Question Mark and Portability

I have a little C# console application that reads a key and checks to see if the key was a question mark:

ConsoleKeyInfo ki = System.Console.ReadKey();
if (ki.ConsoleKey.Oem2) // Do something

I arrived at Oem2 by seeing what value is actually assigned in the debugger, because there is no ConsoleKey code for question mark.

Now I could certainly use ki.KeyChar instead, but the application also needs to respond to certain keys (e.g. media keys) that do not map to characters. It feels inelegant to check both ConsoleKey and KeyChar to determine which key has in fact been pressed. On the other hand, it does not feel safe to rely on Oem2 to always map to ? in all circumstances and regions.

Is it best practice to check both properties to determine which key was in fact pressed?

Any insight into why ConsoleKeyInfo was designed this way is appreciated.

like image 343
Eric J. Avatar asked Feb 28 '12 20:02

Eric J.


People also ask

What is System ConsoleKeyInfo?

ReadKey method. The ConsoleKeyInfo object describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key.

What is the use of ReadKey in C#?

ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

What is console ReadKey in Visual Basic?

Console. ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio . NET. A common use of the ReadKey() method is that you can halt the program execution.


3 Answers

In this case, you will have to check KeyChar == '?'. From MSDN:

Oem2: The OEM 2 key (OEM specific).

So you're just getting lucky in that it happens to be a ? on your equipment.

The ConsoleKeyInfo structure provides KeyChar (a Char value) as well as Modifiers (an enumeration) to help you decide what keys the user had pressed.

like image 123
Yuck Avatar answered Nov 01 '22 17:11

Yuck


I think you should consider what happens when someone has different keyboard layout.

If you want to check for “the key with question mark on my computer”, then use ConsoleKey. But that's probably not a good idea and you should probably adhere to the user's settings and use KeyChar.

But for keys that don't map to to characters (and the user can't remap them by using different keyboard layout), you have to use ConsoleKey.

So, yes, I think you should check both properties in this case.

like image 26
svick Avatar answered Nov 01 '22 19:11

svick


I guess the reason for this design is that Console.ReadKey() relies on a native function (ReadConsoleInput) that returns an array of KEY_EVENT_RECORD structures in case of a keypress, where each key event has an ASCII/Unicode character representation and a virtual key code. Notice the VK_OEM_2 in my previous link - this is where the ConsoleKey.Oem2 value comes from.

like image 2
Yuriy Guts Avatar answered Nov 01 '22 17:11

Yuriy Guts