2011-07-19 47 views
-3

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

char move; 

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

我使用错误的ascii值的上下键吗?

解决

我取代(炭)_getch()至(INT)_getch()和焦炭移动到int移动 然后38和40 ??和80

+0

是什么_getch()?我看到C#就像一个标签。 – Tigran

+0

_getch()是什么? – hcb

+1

您确定这是C#代码吗?根据你对'_getch'的调用,它看起来更像C# –

回答

6

好像你DllImporting MSVCRT.DLL使用_getch()

尝试使用Console.ReadKey()

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

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

} ... 
1

如果我们在谈论一个WinForms应用程序,我会建议您使用Control.KeyDown Event。 “Console.Read()”不适用于WinForms应用程序。

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

+0

它是控制台应用程序 –