I'm trying to make a menu in my c++ console program where a user can hit the up and down arrow keys and this will scroll through the menu options. I got this snippet of code that I could modify to make it do what I want, but its not working.

#define PAGE_UP     73
#define HOME        71
#define END         79
#define PAGE_DOWN   81
#define UP_ARROW    72
#define LEFT_ARROW  75
#define DOWN_ARROW  80
#define RIGHT_ARROW 77
#define F1          59
#define F2          60
#define F3          61
#define F4          62
#define F5          63
#define F6          64
#define F7          65
#define F8          66
#define F9          67
#define F10         68
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
	char KeyStroke;

	cout << "Press Escape to quit." << endl;
	
	do
	{
		KeyStroke =	getch();

		if (KeyStroke == 0)
		{
			KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
			switch (KeyStroke)
			{
			case PAGE_UP:
				cout << "PAGE UP" << endl;
				break;
			case PAGE_DOWN:
				cout << "PAGE DOWN" << endl;
				break;
			case HOME:
				cout << "HOME" << endl;
				break;
			case END:
				cout << "END" << endl;
				break;
			case UP_ARROW:
				cout << "UP ARROW" << endl;
				break;
			case DOWN_ARROW:
				cout << "DOWN ARROW" << endl;
				break;
			case LEFT_ARROW:
				cout << "LEFT_ARROW" << endl;
				break;
			case RIGHT_ARROW:
				cout << "RIGHT_ARROW" << endl;
				break;
			case F1:
				cout << "F1" << endl;
				break;
			case F2:
				cout << "F2" << endl;
				break;
			case F3:
				cout << "F3" << endl;
				break;
			case F4:
				cout << "F4" << endl;
				break;
			case F5:
				cout << "F5" << endl;
				break;
			case F6:
				cout << "F6" << endl;
				break;
			case F7:
				cout << "F7" << endl;
				break;
			case F8:
				cout << "F8" << endl;
				break;
			case F9:
				cout << "F9" << endl;
				break;
			case F10:
				cout << "F10" << endl;
				break;
			default:
				cout << "Some other key." << endl;
			}
		}
		else
			cout << KeyStroke << endl;
	} 
	while (KeyStroke != 27); // 27 = Escape key
}

It works correctly for all the function keys, and normal keys, but for the arrow keys, and home key, it prints out a very weird message like:

$
H

Can anyone tell me whats going wrong? Thanks.

getch() returns 224, not 0, when an arrow key is pressed. So the program has to check for both values (0 and 224)

Also, getch() returns an int, not char.

getch() returns 224, not 0, when an arrow key is pressed. So the program has to check for both values (0 and 224)

Also, getch() returns an int, not char.

What exactly does that mean though?

What exactly does that mean though?

Didn't you write the code you posted? All you have to do is change that if statement and change the data type of KeyStroke from char to int.

Didn't you write the code you posted? All you have to do is change that if statement and change the data type of KeyStroke from char to int.

No. I didn't write this. This was a public snippit I found and tested. I understand basicly how it works, and I understand what you mean by changing the variable type, but what do I need to change the if statement to?

I already told you what to change it to: if (KeyStroke == 0 || KeyStroke == 224)

Thanks for the help :)

This article has been dead for over six months. Start a new discussion instead.