Search code examples
c#arrow-keys

Using Arrow Keys to navigate in the Console


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 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


Solution

  • 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) {
    
    } ...