开发者

Using Arrow Keys to navigate in the Console

开发者 https://www.devze.com 2023-03-21 02:23 出处:网络
Im making a menu. I want to use arrow keys to select from my list. char move; do { move = (char)_getch();

Im making a menu. I want to use arrow keys to select from my list.

char move;

do
{
    move = (char)_getch();
    if (move == 38)
    {
         // Move Indicator Up   
    }
    else if (move == 40)
    {
         // Move开发者_如何学JAVA Indicator Down
    }
}
while (move != 13);

Am i using the wrong ascii values for up and down keys?

SOLVED

i replaced (char)_getch() to (int)_getch() and char move to int move then 38 and 40 to ?? and 80


Seems like you're DllImporting msvcrt.dll to use _getch()

Try using Console.ReadKey()

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

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

} ...


In case we are talking about a WinForms application i would recommend you to use the Control.KeyDown Event. "Console.Read()" doesn't work for WinForms applications.

Update Example for menu navigation with arrow key for console application in C#. >> Sample 1 Sample 2

0

精彩评论

暂无评论...
验证码 换一张
取 消