Thread: Arrow Key menus in console mode

  1. #1
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35

    Question 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.
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    178
    I'm not sure as I have not got around to trying it yet, but i believe you could make it so that you can use the getch to determine the keypress as up or down, then depending have it run an if statement to determine where it is and then move it either up or down. I know this probably isn't much help but hopefully it will strike some thoughts. (:

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    30

    I'll give you a hint

    Try this code and see what it does, it may help you:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    	char pressedkey;
    	while(pressedkey!='\r')
    	{
    		if(kbhit())
    		{
    			pressedkey=getch();
    			printf("%c",pressedkey);
    		}
    	}
    	return 0;
    }
    Also, in case you're wondering, the ASCII-numbers for up, down, left and right
    (as ints) are (not sure of which is which) 72, 75, 77 and 80.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I'll give you a hint

    Originally posted by ninja
    Try this code and see what it does, it may help you:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    	char pressedkey;
    	while(pressedkey!='\r')
    	{
    		if(kbhit())
    		{
    			pressedkey=getch();
    			printf("%c",pressedkey);
    		}
    	}
    	return 0;
    }
    Also, in case you're wondering, the ASCII-numbers for up, down, left and right
    (as ints) are (not sure of which is which) 72, 75, 77 and 80.
    This code has a couple of bits wrong with it.

    - getch() returns an int, you have defined pressedkey as a char. This will cause problems if and when you need to test for EOF

    - pressedkey isn't initialised before it is tested for a specific value. At this point, you have no idea what pressedkey is equal to.

    - I don't believe this will help you get the value of the arrow keys.
    When an arrow key is pressed, you need to call getch() twice. This is because the first read will get you a special value that denotes you must do another read. Here's some code to help explain. Run it, and press an arrow key. You should find that it displays 2 lines

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void){	
    	
    	int pressedkey;
    	int i = 0;
    	
    	printf ("Press any key.\n");
    	while (1) 
    	if ((pressedkey = getch()) != EOF)
    	{
    		printf("%d %04x\n", i++, pressedkey);
    	}
    	return 0;
    }
    
    /* Program output:
    Press any key.
    0 0000    <-- left arrow
    1 004b
    2 0000    <-- right arrow
    3 004d
    */
    Yes, I know it's in C, my C++ is a bit too rusty!
    Last edited by Hammer; 05-31-2002 at 07:54 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    30

    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)
    Last edited by ninja; 05-31-2002 at 08:09 AM.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: No, my code works perfectly

    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;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    30

    (edited)

    [EDIT]Sorry, didn't read your post to thoroughly. You're right.
    That's what I get from being up to late... Please ignore the incorrect and weird facts of my posts. I removed the rest of the contents of this post, since it was erroneous.[/EDIT]
    Last edited by ninja; 05-31-2002 at 09:31 AM.

  8. #8
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Check out this thread: http://www.cprogramming.com/cboard/s...threadid=18824

    If you scroll down red_baron has an attachment, keys.h. It has all the keys you will ever need defined. (i don't like his naming conventions though)

  9. #9
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    thanks for all the replies
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-22-2008, 07:20 PM
  2. console mode and service mode
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-01-2008, 01:42 AM
  3. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  4. Arrow key reading
    By sufthingol in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 04:08 PM
  5. Creating fractals in C++ console mode
    By speedy in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 01:02 PM