0

我在控制台上为 shift+number 字符获取正确的输入击键代码时遇到了一些麻烦。例如,使用:

cki = Console.ReadKey(True)
Console.WriteLine("You pressed the '{0}' key.", cki.Key)

如果我按 shift+2,我希望得到 ascii 64(用于“@”字符),但我得到 50(用于“2”字符)。

现在,我知道你可以得到按下键的修饰符,但这意味着我必须为这样的键编写所有特殊情况,这似乎不正确。

我需要这个功能或类似的功能,因为它能够在按下按键时读取按键,而无需按 Enter,否则我只需使用 console.read。我肯定错过了一些东西。谁能告诉我我错过了什么?

4

2 回答 2

1

It is pretty important to distinguish between keys and characters. The key is the same anywhere in the world, the one on the top row at the left. You can rely on that key always producing ConsoleKey.D2

The character is however very different, it greatly depends on the active keyboard layout. A Northern American user presses Shift+2. A French user presses AltGr+0. A German user presses AltGr+Q. A Spanish user presses AltGr+2. Etcetera.

If you care only about the key then use ConsoleKeyInfo.Key, you do so for all non-typing keys like the function keys for example. Perhaps the typical gaming WASD keys. If you care only about the character, like @, then use ConsoleKeyInfo.KeyChar.

于 2013-11-12T19:59:07.067 回答
1

您正在寻找KeyChar属性,它返回实际字符​​而不是按下的物理键。
您可能希望将其转换为int.

于 2013-11-12T18:00:41.990 回答