-3

我正在制作菜单。我想使用箭头键从我的列表中进行选择。

char move;

do
{
    move = (char)_getch();
    if (move == 38)
    {
         // Move Indicator Up   
    }
    else if (move == 40)
    {
         // Move Indicator Down
    }
}
while (move != 13);

我是否为向上和向下键使用了错误的 ascii 值?

解决了

我将 (char)_getch() 替换为 (int)_getch() 并将 char move 替换为 int move 然后将 38 和 40 替换为 ?? 和 80

4

2 回答 2

6

好像你正在 DllImporting msvcrt.dll 使用 _getch()

尝试使用 Console.ReadKey()

ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.UpArrow) {

} else if (keyInfo.Key == ConsoleKey.DownArrow) {

} ...
于 2011-07-19T11:41:16.250 回答
1

如果我们谈论的是 WinForms 应用程序,我建议您使用Control.KeyDown 事件。“Console.Read()”不适用于 WinForms 应用程序。

使用 C# 中控制台应用程序的箭头键更新 菜单导航示例。>>样品 1 样品 2

于 2011-07-19T11:33:42.083 回答