Arrow Key menus in console mode
hi.
What i am trying to do is make a menu for a game (in console) that looks like this:
GAME TITLE BLAHBLAH
+ Play Game
Options
Exit
now i want the program to sit at that menu, and accept the up and down arrow keys to move the plus sign down , or up respectively, to highlight different options. i also want it so when the user presses enter, it goes to the menu/whatever that is currently selected.
I tried doing this myself, using some sample code that kind of relates to it, and i came close... but i just couldn't do it. I can't post any code because i didn't save it out of anger ;)
if anyone could give me some pointers on how to achieve something like this, i would appreciate it lots.
No, my code works perfectly
I've tried and compiled it even. Used it quite extensively in various forms even.
getch() gets a char from the buffer (in this case, the key you pressed). And sure, pressed key is uninitialized, but you can set to pretty much anything you want to as long as it's not the "ending key".
And it does help to get the numbers of the keys. Like this:
Code:
...
if(pressedkey==72)
{
//Whatever code
}
...
The above code will execute "whatecer code" when one of the arrow keys (not sure which) is pressed. That's a fact I know for sure.
(might not work with all compilers, though)
Re: No, my code works perfectly
Quote:
Code:
...
if(pressedkey==72)
{
//Whatever code
}
...
The above code will execute "whatecer code" when one of the arrow keys (not sure which) is pressed. That's a fact I know for sure.
(might not work with all compilers, though)
After you press the arrow key, your code is only getting the value 72 from getch() when it calls it a second time. It is doing a second call because you have encased it in a loop. If you only do one call to getch(), it won't work. That is what I'm highlighting.
This is your code, without the loop. It only does one getch() call, then exits. Try it, and you find you never get the text Yippee printed on the screen, no matter what arrow key you press.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{ int pressedkey;
pressedkey=getch();
printf("%c",pressedkey);
if (pressedkey == 72)
printf ("Yippee\n");
return 0;
}