ConsoleKeyInfo.Key returns 231 for number keys 0..9

Dani Stutz 1 Reputation point
2021-12-14T17:42:20.63+00:00

I have a program which uses the ReadKey function. The key property of the returned ConsoleKeyInfo structure is 231 for all the number keys 0..9 on the regular keyboard and the numeric key pad.
For the OnScreen keyboard the key returns 48..57.

The enum ConsoleKey (https://learn.microsoft.com/en-us/dotnet/api/system.consolekey?view=net-6.0) has values 48..57 (D0..D9) and 96..105 (NumPad0..NumPad9)
Is there a setting or an option to change this behaviour or is it a bug?

OS: Windows 10 Enterprise for Virtual Desktop
Version: 20H2
OS Build: 19042.1348

Keyboard: QWERTZ (Swiss German)
Language: Deutsch (Schweiz)
Region: de-CH

Small sample program:

static void Main(string[] args)
{
Console.WriteLine("Hello to Key Console");
Console.WriteLine("Press ESC to exit.");
bool exitProgram = false;
while (!exitProgram)
{
if (Console.KeyAvailable)
{
// https://learn.microsoft.com/en-us/dotnet/api/system.consolekeyinfo?view=net-6.0
var keyinfo = Console.ReadKey();
Console.WriteLine();
Console.WriteLine($"KeyChar: {keyinfo.KeyChar}");
Console.WriteLine($"Key: {(long)keyinfo.Key}");
Console.WriteLine($"Modifier: {(long)keyinfo.Modifiers}");

                if (keyinfo.Key == ConsoleKey.Escape)  
                    exitProgram = true;  
            }  
        }  
    }  

Regular:
157544-keyconsole-virtual-regular0-9.jpg

OnScreen:
157552-keyconsole-onscreen-0-9.jpg

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,022 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2021-12-14T19:41:17.59+00:00

    231 is the unicode prefix character. so your numeric keys are probably in a special mode (alt key on)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.