selection using arrow keys?

i want to know that how i can select option using arrow keys
like
1 input
2 output
3 exit

if i press down arrow then cursor goto 2 then 3 and press enter select that option
and how i can show the blinking cursor
and underline the first word of every option like i underline then o underline
Every key on keyboard has got an ASCII value.Ascii value for UP arrow key is 72,down is 80, left is 75 and right is 77.With this information you can make your program.
Here's something akin to what Pterodactyl was talking about, utilizing Virtual-key Codes found here:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

- as opposed to ASCII values. But you could use ASCII in a similar manner.

The key here (no pun intended) is to actually understand what is happening with the gotoXY, and the GetAsyncKeyState. Don't just copy/paste; take it, play around with it if it's new to you, have fun in the console and learn how to make it do what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Menu example for tjnapster
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // used for goto
COORD CursorPosition; // used for goto

void gotoXY(int,int); // function defined below if this is new to you.

int main()
{
	int menu_item=0, run, x=7;
	bool running = true;
	
	gotoXY(18,5); cout << "Main Menu";
	gotoXY(18,7); cout << "->";
	
	while(running)
	{
                gotoXY(20,7);  cout << "1) Input";
		gotoXY(20,8);  cout << "2) Output";
		gotoXY(20,9);  cout << "3) ...";
		gotoXY(20,10); cout << "4) ...";
		gotoXY(20,11); cout << "Quit Program";

		system("pause>nul"); // the >nul bit causes it the print no message
		
		if(GetAsyncKeyState(VK_DOWN) && x != 11) //down button pressed
			{
				gotoXY(18,x); cout << "  ";
				x++;
				gotoXY(18,x); cout << "->";
				menu_item++;
				continue;
				
			}
			
		if(GetAsyncKeyState(VK_UP) && x != 7) //up button pressed
			{
				gotoXY(18,x); cout << "  ";
				x--;
				gotoXY(18,x); cout << "->";
				menu_item--;
				continue;
			}
			
		if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed
			
			switch(menu_item){
				
				case 0: {
					
					gotoXY(20,16);
					cout << "You chose Input...     ";
					break;
				}
					
					
				case 1: {
					gotoXY(20,16);
					cout << "You chose Output...     ";
					break;
				}
					
				case 2: {
					gotoXY(20,16);
					cout << "You chose Option 3...     ";
					break;
				}
					
				case 3: {
					gotoXY(20,16);
					cout << "You chose Option 4...     ";
					break;
				}
					
				case 4: {
					gotoXY(20,16);
					cout << "The program has now terminated!!";
					running = false;
				}
				
			}
				
		}		
		
	}
	
	gotoXY(20,21);
	return 0;
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}


I'm sure many people will suggest you steer clear of system() commands, but this will do for now.
If you were to use ASCII, as Ptero suggested, you could avoid system().
Topic archived. No new replies allowed.