I wrote this recently, it should basically answer your question.
Code:
/* ******************************************
Name: Detecting Extended Characters in DOS
Author: W---------------- ** Not a chance **
Date: 06/10/05 12:43
Description: Shows how a programmer may
use functions in conio.h to detect extended
characters while still allowing the ability
to read non-extended characters.
****************************************** */
#include <iostream.h>
#include <conio.h>
const char UPKEY = 72;
const char LEFTKEY = 75;
const char RIGHTKEY = 77;
const char DOWNKEY = 80;
const char INSERTKEY = 82;
const char DELETEKEY = 83;
const char HOMEKEY = 71;
const char ENDKEY = 79;
const char PGUPKEY = 73;
const char PGDNKEY = 81;
const char SPACE = 32;
const char ENTER = 13;
const char TAB = 9;
const char BSPACE = 8;
const char ESC = 27;
int main()
{
bool isExtended;
char keyPress;
cout << "Press any key on your keyboard. This program should read" << endl
<< "and echo them all accordingly, with the exception of some" << endl
<< "certain keys, as they were either not defined or have" << endl
<< "specific functions within the DOS window." << endl << endl;
do
{
keyPress = getch();
if (int(keyPress) == -32 || int(keyPress) == 0) // Values returned by
isExtended = true; // Extended Characters.
else
isExtended = false;
cout << "You pressed ";
if (isExtended == true)
{
keyPress = getch(); // Reads in the second value of the
// Extended Character.
switch(keyPress)
{
case UPKEY:
cout << "the UP key." << endl;
break;
case DOWNKEY:
cout << "the DOWN key." << endl;
break;
case LEFTKEY:
cout << "the LEFT key." << endl;
break;
case RIGHTKEY:
cout << "the RIGHT key." << endl;
break;
case INSERTKEY:
cout << "the INSERT key." << endl;
break;
case DELETEKEY:
cout << "the DELETE key." << endl;
break;
case HOMEKEY:
cout << "the HOME key." << endl;
break;
case ENDKEY:
cout << "the END key." << endl;
break;
case PGUPKEY:
cout << "the PAGE UP key." << endl;
break;
case PGDNKEY:
cout << "the PAGE DOWN key." << endl;
break;
default:
cout << "an undefined extended character." << endl;
break;
}
}
else if (isExtended == false)
{
switch(keyPress)
{
case SPACE:
cout << "the SPACE key." << endl;
break;
case BSPACE:
cout << "the BACKSPACE key." << endl;
break;
case ENTER:
cout << "the ENTER key." << endl;
break;
case TAB:
cout << "the TAB key." << endl;
break;
case ESC:
cout << "the ESCAPE key." << endl;
break;
default:
cout << "the [" << keyPress << "] key." << endl;
break;
}
}
} while (1);
return 0;
}
Though I'm sure there are functions in some header that already did this for you. Perhaps windows.h or something.
Oh well, look around or do something like this.
...and for the hell of it, here is a program that uses the arrowkeys:
Code:
/* *************************************************
Name: Active Movement Interfaces in DOS
Author: W---------------
Date: 06/10/05 18:14
Description: Demonstrates the use of the gotoxy()
function and getch() fuction to create an active
interface in a standard DOS program.
***************************************************/
#include <iostream.h>
#include <conio.h> // Should contain a standard gotoxy() function, but
// mine doesn't for some reason, so I defined it
// accessing Windows API directly.
#include <windows.h> // Needed for the gotoxy() function I used.
void gotoxy(int x, int y);
int main()
{
const char UPKEY = 72;
const char LEFTKEY = 75;
const char RIGHTKEY = 77;
const char DOWNKEY = 80;
int horiAXIS = 2;
int vertAXIS = 2;
int keyPress;
cout << "Press the arrow keys to move the \"o\"." << endl << endl;
cout << " o";
while (1)
{
keyPress = getch();
switch(keyPress)
{
case UPKEY:
if (vertAXIS > 1)
vertAXIS = vertAXIS - 1;
break;
case DOWNKEY:
vertAXIS = vertAXIS + 1;
break;
case LEFTKEY:
if (horiAXIS > 0)
horiAXIS = horiAXIS - 1;
break;
case RIGHTKEY:
horiAXIS = horiAXIS + 1;
break;
}
cout << " "; //Clears the last character.
gotoxy(horiAXIS,vertAXIS);
cout << "o"; //Creates the new character.
gotoxy(horiAXIS,vertAXIS); //Returns to standard space after print.
}
return 0;
}
// I didn't write the following gotoxy() function. It can be found at:
// http://spike.scu.edu.au/~jmaltby/c.html
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}