That would depend on your operating system and compiler.
So which do you have?
Try to be more specific than "windows" and "borland", as there are many versions of each.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Try this simplest program, look at these codes on your screen:
#include <stdio.h"
#include <conio.h>
/* Press Esc to quit */
#define ESC 27
int main()
{
int ch;
while ((ch=getch()) != ESC)
{
printf("%d",ch);
while (kbhit())
{
printf(" %d",getch());
}
printf("\n");
}
printf("ESC %d\n",ch);
return 0;
}
Special keys are presented by two codes, as usually the 1st is 0 (zero).
So you need two getch call to accept these keys.
Take a pen and a piece of paper, start the program then press your favorite keys...
This program works not only with unfading Turbo C...
Good luck!
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Actually, if you need to simply accept input, you don't need kbhit() at all:
c2 = 0; // reset c2 to 'nothing'
c1 = getch(); // wait for a key
if (c1 == 0) // check if it's a function key
{
c2 = getch(); // it is, get the function value
}
// if c2 is not 0, a function key was pressed
kbhit() is useful when you want the program to continuously run and when a key is pressed do something different. For example:
if (kbhit()) // check to see if a key was pressed
{ // a key was pressed so we interrupt the standard
// programming cycle
c2 = 0; // reset c2 to 'nothing'
c1 = getch();
if (c1 == 0)
{
c2 = getch();
}
// if c2 is not 0, a function key was pressed
...
}
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
About kbhit(): look at my program example then try to print the second byte of a special key without kbhit() at the same line...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
About kbhit(): look at my program example ...
I did, and don't see why you need a kbhit() for that. A simple test for 0 would work.... then try to print the second byte of a special key without kbhit() at the same line...
Simple without the kbhit() . Look atmy code and make the simple change...
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Alas, not all special keys have 0 as a 1st byte of getch() pair (F11, F12 on Windows XP in console applications, for example)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Yeah, I know. The concept is still correct. As a matter of fact, the first byte for those keys has been 0xE0 long before XP was created.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Will we really dispute about concepts of so fundamental issue as getch, kbhit and other conio stuff using?..
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
And what new information has been added by resurrecting a 3-year old thread? I see nothing that hasn't already been mentioned.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944