I found the following code in the 'Code snippets' section( written by nanodano).

i tried compiling it in my Dev-Cpp compiler, and i got the output as shown in the attachment for up, left, down and right arrow keys respectively. All other defined keys i.e., F1 to F10 gave the proper output-although F11, F12, PageUp, Page Down, Home and End gave similar output as the arrow keys.

Also i didn't understand why we have to use getch() twice. Can someone explain, please?

/* This program shows how to pick up the scan codes from a keyboard */

/* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
#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
}
Attachments Scan_output.JPG 23.09 KB

The posted pic is too tiny to see the output properly, so thought I'd post another pic. Hope this helps.

Attachments Scan_output.JPG 13.39 KB

Silly me! Just realised that pageup, pgdn, home, end and the arrow keys refer to the ones on the numpad. I was trying the usual arrow keys till now. Works fine with NumLock turned off.:$ And F11 and F12 haven't even been defined. *more embarassed*
Now if someone would only explain what the other getch() is doing there. . . :)

Each function and arrow key is really two characters, a \0 followed by another character. Note the code:

KeyStroke = getch();
if (KeyStroke == 0)
{
    KeyStroke = getch(); 
    switch (KeyStroke)

Get a keystroke with the first ]getch() call
If that value is a 0, get the next keystroke...

what should be done to get the input from normal arrow keys? (not the ones on the num-pad) Do they have different scan codes?

what should be done to get the input from normal arrow keys? (not the ones on the num-pad) Do they have different scan codes?

Why not write a short test program and find out. You have enough information to do that.

I used the dev-cpp debugger to add a watch to the variable keystroke and found out that when i pressed the uparrow, the first character wasn't zero, but some negative vale. So by changing the code from if(keystroke==0) to if(keystroke<=0) , i finally got it.

Thanks WaltP

I used the dev-cpp debugger to add a watch to the variable keystroke and found out that when i pressed the uparrow, the first character wasn't zero, but some negative vale. So by changing the code from if(keystroke==0) to if(keystroke<=0) , i finally got it.

Thanks WaltP

That's very weird. What value (HEX) was it? Maybe 0xE0? By specifying <=0 you may have turned some legitimate keys into function keys. There are some keys that actually have a negative value if you program the code wrong.

The first character that got stored in keystroke looked like this: -32'à'. I dunnno what that 'à' thing is. I just noticed that it was a negative value, so i modified the code to if(keystrike<=0) . The second value that gets stored in keystroke at the execution of the second getch() is the normal hexadecimal scan code value.

It works perfectly well now. Maybe it's something related to this keyboard only?:confused:

This question has already been answered. Start a new discussion instead.