Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,674 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,206 people online right now. Registration is fast and FREE... Join Now!




C++ or C Console application arrow-key driven menu

 
Reply to this topicStart new topic

C++ or C Console application arrow-key driven menu

cmolson
post 9 Jun, 2007 - 05:05 PM
Post #1


New D.I.C Head

*
Joined: 9 Jun, 2007
Posts: 3


My Contributions


Hey guys, I have been trying to find an example for a menu. I have seen programs in that when run in the console use the arrow keys to select a menu item. I have a menu like so:

1. Enter info
2. Save/Load info
3. Display current info
4. Reset Values
5. Exit Program

Instead of having the user type the number followed by enter, I would like the current menu item to be highlighted and the user can change the highlighted menu with the arrow keys. Then when the user hits enter it should go to the corresponding menu item function.

I have tried searching the net and this site but haven't found anything. If anyone knows what this kind of menu is called, or knows somewhere/something to help get me started that would be great!

Thanks

Christian
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Jun, 2007 - 06:05 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


This will likely be dependent on your platform/compiler...unless you want to trap the arrow keys and perform actions based on that.
User is offlineProfile CardPM

Go to the top of the page

cmolson
post 9 Jun, 2007 - 06:12 PM
Post #3


New D.I.C Head

*
Joined: 9 Jun, 2007
Posts: 3


My Contributions


I am using Visual C++ 2005 Express from Microsoft currently. I would like to eventually switch to a linux IDE however and use the GNU compiler for C/C++.
User is offlineProfile CardPM

Go to the top of the page

obaid
post 13 Apr, 2008 - 03:25 AM
Post #4


New D.I.C Head

*
Joined: 13 Apr, 2008
Posts: 1

wub.gif wub.gif i want to ude arrow keys in my atm code in c++
but cant know.please help me as early as possible.
i submitt my project after 5 hour
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 13 Apr, 2008 - 05:32 AM
Post #5


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


I think I can help. First off I should warn you that this will be platform dependant (i.e. work only on windows).

Start by looking at my tutorial on the windows console output. This has some very useful functions in it. Just keep in mind that with C++ the header should be "cstdio" not "stdio.h" but it would still be "windows.h".

The reason I bring this up is that color allows you to highlight a particular choice as the user presses arrows.

The next part is to react to console input events! (you could have a mouse driven menu if you wanted). The basic functionality is expressed in this example from MSDN.

What you want to do for now is to copy and paste that example and compile it and make sure it works for you. Then you want to change the KeyEventProc to print out the value of the wVirtualScanCode (it helps if you make the output in HEX -- use the printf's %X format identifier). Then then you run the program and press keys you should see the scan code for the various keys. Make sure your record the values for the arrow keys (the should be something like up=0048, down 0050, left 004b, right 004d). You should also find that the last two digits are the same for the number pad arrow keys and the gray keys).

Once you know the scan codes for the keys you can move on to programming your menu. You will want to use the technique shown in the example to trap input events. Then you look for those scan codes. When they are triggered you have your menu change what is highlighted (i.e. you print the old menu item in normal colors, and the newly hilighted value in new colors using the ConPrintAt function from my snippet (or there is another snippet with a printfAt function however that function is a little broken since it only prints lines upto 255 characters -- past that it will overload the buffer).

Anyway... that is a lot to chew up, but in the end the logic is really not very hard, though the windows API stuff makes it look very complicated. I will switch over to my laptop and see if I can make a little example.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 13 Apr, 2008 - 06:30 AM
Post #6


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


BTW a slight oversight on my part. you really should be looking at the virtual key codes and not the scan codes. The scan codes may depend upon the keyboard being used (though in my experience they have always been the same) where as the virtual key code should be device independant (meaning that any keyboard or keyboard mapping should return the right values).

This is handy if there are keys that are mapped (for example I once had a keyboard without an H key and so I mapped that H to F12... made typing rather hard... but it worked -- mostly). So for example if the user maps they arrow keys to "wasd" then your program would still work.

Anyway still working on an example. For now I was playing around with the MSDN example and had changed the key event handler to:
CODE

VOID KeyEventProc(KEY_EVENT_RECORD ker)
{
    printf("Key event: ");

    if(ker.bKeyDown) {
        printf("key pressed: ScanCode: %X %X ", ker.wVirtualScanCode, ker.wVirtualKeyCode);
    } else {
        printf("key released\n");
    }
}
So that I could look at the various values.

Turns out that windows.h defines constants for all of the virtual key codes. which is rather handy.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 13 Apr, 2008 - 06:37 AM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


...you know, I just noticed that the original post was from Jun 2007... Ah well, I will still write out a little snippet or tutorial. Man I hate it when I do that.

I am sorry but I don't think there is time to really help poor obaid.
User is offlineProfile CardPM

Go to the top of the page

cmolson
post 13 Apr, 2008 - 04:21 PM
Post #8


New D.I.C Head

*
Joined: 9 Jun, 2007
Posts: 3


My Contributions


lol yeah this was from last summer.


buttt.... I had given up on the idea lol, but this summer I will be taking more classes and I stillw ant to give this a try. So thanks for posting this! I appreciate it!


Thanks
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 14 Apr, 2008 - 05:51 AM
Post #9


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


kk... then I will try to find time to finish that example... sort of lost my steam on it when I realized it twas so old.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:25AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month