0

I have a nice menu system and I would like to move an arrow around defending on selection. I'm testing this out so I only have a situation build for the down key, but eventually I would have down and up arrows.

I got it to work because I figured out after I press the down key, choice is 224. I ran into a new problem where the UP arrow key is also 224...

char    selector[4] = {'>',' ',' ',' '};
while (1)
{
    DisplayMenu();
    printf("        Main Menu:               \n");
    printf("        %c Log in                   \n", selector[0]);
    printf("        %c Display High Scores      \n", selector[1]);
    printf("        %c Start Game               \n", selector[2]);
    printf("        %c Exit                     \n", selector[3]);
    choice = getch();
    switch (choice)
    {
    case 224:
        for (int i = 0; i < 4; i++)
        {
            if (selector[i] == '>')
            {
                selector[i] = ' ';
                i++;
                selector[i] = '>';
            }
        }
        break;
    }
    system("cls");
}
return numberChoice;

My theory was when the down key is pressed, move the '>' over in the char array and then when the menu is displayed, it would show either the '>' or a space, giving it the illusion that your moving around in the menu options. Of course, I can't seem to find a way to detect what the down arrow was. Any help?

Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • I don't believe there's any built-in way to do that. You'd have to use an external library or whatever functionality is provided by your OS. – dandan78 Mar 15 '16 at 21:24
  • 1
    Where did you get the magic number "224" from? Tell us what OS you're using. There is no standard way to read these keys in C++. – PaulMcKenzie Mar 15 '16 at 21:28
  • 224 is the value of choice after I pressed the down key. It turns out that when I press the up arrow key, it is also 224.. – Robolisk Mar 15 '16 at 22:13
  • This explains the special processing necessary for the arrow keys on Windows (the 0xe0 or 224) http://stackoverflow.com/q/28122738/920069 – Retired Ninja Mar 15 '16 at 22:44

1 Answers1

0

It depends on the platform.

I recommend you look at the source code of the OpenGL library glfw, which implements abstractions to handle keyboard events.

This search on GitHub will show you how they implement GLFW_KEY_DOWN for various platforms.

Escualo
  • 40,844
  • 23
  • 87
  • 135