Using arrow keys and mouse in console window

Hello

I am making a program that consists of a menu. One way to do this is as follows:

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
.
.
.

int choice;

cout << "\n\t\tMENU\n";
cout << "\n1. Play\n";
cout << "2. Options\n;
cout << "3. Credits\n";
cout << "4. Help\n";
cout << "5. Exit\n";

cout << "\nEnter your choice: ";
cin >> choice;

switch(choice)
{
   case 1:{
              // code goes here
          } break;

   case 2:{
              // code goes here
          } break;

   case 3:{
              // code goes here
          } break;

   case 4:{
              // code goes here
          } break;

   case 5:{
              // code goes here
          } break;

   default : cout << "\nInvalid input!";
}

.
.
. 


But in this case we don't use arrows or the mouse to select or highlight our choice. I want to do this menu system using graphics that uses arrows and the mouse. So how to do that?

NOTE: I am not asking for FULL codes or anything like that. What I want to know is how to use the arrows and the mouse.


Thanks
@The illusionist mirage

http://v2.cplusplus.com/forum/general/96166/

Here I showed how to create a menu that uses the up/down arrow keys, for selection. I'm not sure how to use
the mouse pointer in the console, but I sure would be interested in that also.
Last edited on
@The illusionist mirage

Your request intrigued me, so I checked around, and found this. I added to it a small menu, and printed out the mouse x/y coordinates. Hopefully, this is enough to get you started.
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Reading Input Buffer Events.cpp : main project file.

#include <iostream>
#include <stdio.h>
#include <windows.h>


HANDLE hStdin; 
DWORD fdwSaveOldMode;

void ErrorExit(LPSTR);
void KeyEventProc(KEY_EVENT_RECORD); 
void MouseEventProc(MOUSE_EVENT_RECORD); 
void ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
void GetMousePosWin(MOUSE_EVENT_RECORD);
void gotoXY(int x, int y);

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;



int main() 
{ 
	DWORD cNumRead, fdwMode, i; 
	INPUT_RECORD irInBuf[128]; 
	int counter=0;

	// Get the standard input handle. 

	hStdin = GetStdHandle(STD_INPUT_HANDLE); 
	if (hStdin == INVALID_HANDLE_VALUE) 
		ErrorExit("GetStdHandle"); 

	// Save the current input mode, to be restored on exit. 

	if (! GetConsoleMode(hStdin, &fdwSaveOldMode) ) 
		ErrorExit("GetConsoleMode"); 

	// Enable the window and mouse input events. 

	fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT; 
	if (! SetConsoleMode(hStdin, fdwMode) ) 
		ErrorExit("SetConsoleMode"); 

	// Loop to read and handle the next 100 input events. 
	gotoXY(33,10);
	std::cout << "\xDB Menu Item 1";
	gotoXY(33,12);
	std::cout << "\xDB Menu Item 2";
	gotoXY(33,14);
	std::cout << "\xDB Menu Item 3";
	gotoXY(33,16);
	std::cout << "\xDB Quit";
	while (!counter)//++ <= 200) 
	{ 
		// Wait for the events. 

		if (! ReadConsoleInput( 
			hStdin,      // input buffer handle 
			irInBuf,     // buffer to read into 
			128,         // size of read buffer 
			&cNumRead) ) // number of records read 
			ErrorExit("ReadConsoleInput"); 

		// Dispatch the events to the appropriate handler. 

		for (i = 0; i < cNumRead; i++) 
		{
			switch(irInBuf[i].EventType) 
			{ 
			case KEY_EVENT: // keyboard input 
				KeyEventProc(irInBuf[i].Event.KeyEvent); 
				break; 

			case MOUSE_EVENT: // mouse input 
				MouseEventProc(irInBuf[i].Event.MouseEvent);
				gotoXY(33,8);
				GetMousePosWin(irInBuf[i].Event.MouseEvent);
				break; 

			case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing 
				ResizeEventProc( irInBuf[i].Event.WindowBufferSizeEvent );
				break; 

			case FOCUS_EVENT:  // disregard focus events 

			case MENU_EVENT:   // disregard menu events 
				break; 

			default: 
				ErrorExit("Unknown event type"); 
				break; 
			} 
		}
	} 

	// Restore input mode on exit.

	SetConsoleMode(hStdin, fdwSaveOldMode);

	return 0; 
}

VOID ErrorExit (LPSTR lpszMessage) 
{ 
	fprintf(stderr, "%s\n", lpszMessage); 

	// Restore input mode on exit.

	SetConsoleMode(hStdin, fdwSaveOldMode);

	ExitProcess(0); 
}

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

	if(ker.bKeyDown)
		printf("key pressed");
	else printf("key released");
}

VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
{
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif

	gotoXY(33,6);
	printf("Mouse event:                                               ");

	switch(mer.dwEventFlags)
	{
	
	case 0:
		if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
		{
			gotoXY(46,6);
			printf("left button pressed");
		}
		else if(mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
		{
			gotoXY(46,6);
			printf("right button pressed");
		}
		else
		{
			gotoXY(46,6);
			printf("button press");
		}
		break;
	case DOUBLE_CLICK:
		gotoXY(46,6);
		printf("double click");
		break;
	case MOUSE_HWHEELED:
		gotoXY(46,6);
		printf("horizontal mouse wheel");
		break;
	case MOUSE_MOVED:
		gotoXY(46,6);
		printf("mouse moved");
		//gotoXY(12,13);
		//GetMousePosWin();
		break;
	case MOUSE_WHEELED:
		gotoXY(46,6);
		printf("vertical mouse wheel");
		break;
	default:
		gotoXY(46,6);
		printf("unknown");
		break;
	}
	
}

// get Window pos through windows api
void GetMousePosWin(MOUSE_EVENT_RECORD mer)
{
	int x,y;
	// creates the handle i need to use
	//HANDLE OutputH;
	INPUT_RECORD Inrec;
	DWORD evRead;
	HANDLE hStdIn;
	DWORD dwMode;
	bool Captured=false;
		
	hStdIn = GetStdHandle(STD_INPUT_HANDLE);
	dwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;

	if( SetConsoleMode( hStdIn, dwMode | ENABLE_MOUSE_INPUT) == TRUE)

		GetConsoleMode(hStdIn, &dwMode);
		SetConsoleMode(hStdIn, (dwMode & (ENABLE_MOUSE_INPUT)));

		// grab the handle to the console so i can use it
		//OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
		//printf("Strated"); //Debug line.

		do
		{
			PeekConsoleInput(hStdIn, &Inrec, 1, &evRead);

			if( evRead ) 
			{ 
				ReadConsoleInput(hStdIn, &Inrec, 1, &evRead);
				x= Inrec.Event.MouseEvent.dwMousePosition.X ;
				y= Inrec.Event.MouseEvent.dwMousePosition.Y ;
				switch (Inrec.EventType )
				{

				case MOUSE_EVENT:
					{
						Captured = true;
						gotoXY(33,8);
						std::cout << "x->  " << x << " ";
						gotoXY(43,8);
						std::cout << "y->  " << y << " ";
						break;	
					}
				}
			}

		}while(!Captured);
	
	if((x==33 && y==10) && mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
	{
		gotoXY(47,10);
		std::cout << "\xFB";
		gotoXY(33,21);
		std::cout << "You picked menu one";
		Sleep(1200);
		gotoXY(47,10);
		std::cout << " ";
		gotoXY(33,21);
		std::cout << "                   ";
	}
	if((x==33 && y==12) && mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
	{
		gotoXY(47,12);
		std::cout << "\xFB";
		gotoXY(33,21);
		std::cout << "You picked menu two";
		Sleep(1200);
		gotoXY(47,12);
		std::cout << " ";
		gotoXY(33,21);
		std::cout << "                   ";
	}
	if((x==33 && y==14) && mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
	{
		gotoXY(47,14);
		std::cout << "\xFB";
		gotoXY(33,21);
		std::cout << "You picked menu three";
		Sleep(1200);
		gotoXY(47,14);
		std::cout << " ";
		gotoXY(33,21);
		std::cout << "                     ";
	}
	if((x==33 && y==16) && mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
	{
		gotoXY(40,16);
		std::cout << "\xFB";
		gotoXY(33,21);
		std::cout << "You picked Quit";
		Sleep(1200);
		gotoXY(40,16);
		std::cout << " ";
		gotoXY(28,21);
		exit(0);
	}
}

VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
{
	printf("Resize event\n");
	printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}
Last edited on
Topic archived. No new replies allowed.