It really depends on your standard lib (most likely from your compiler). As such what you will get will very from OS to OS and compiler to compiler.
Your standard lib takes the OS calls to read keys. It then converts the value returned from the OS to values that get returned from the C stdin. Some mappings are straightforward like A-Z but others like F1-F12, page up/down, and ALT-Keys are not.
There are a few things that most people agree on:
- A-Z map to ASCII 65-90
- a-z map to ASCII 97-122
- 0-9 map to ASCII 48-57
- Standard punctuation map to ASCII where they exist (.,"! etc)
- Control A-Z map to ASCII control codes 1-26
- Return/Enter maps to ASCII 13
- ESC maps to ASCII 27
- TAB maps to ASCII 9
Most times they agree on these as well:
- Backspace maps to ASCII 8
- Delete maps to ASCII 127
This is why you sometimes get a value for some keys but not all of them.
Based on your function call getche() and your tag turboc++, I suspect you are using Turbo C++. Turbo C++ uses the old DOS method (as does GWBasic and a lot of other programs from the DOS days).
In the DOS method you would get a 0 followed by a second character that was the key pressed. So you would read stdin, if it was 0 read stdin again, and then handle that code as the key pressed in a switch statement.
For example left arrow is "\0" followed by a "K".
Here are a number of common keys using the DOS method.
a=getche();
if(a==0)
{
a=getche();
switch(a)
{
case 'H': printf("Up arrow\n");break;
case 'P': printf("Down arrow\n";break;
case 'K': printf("Left arrow\n";break;
case 'M': printf("Right arrow\n";break;
case ';': printf("F1\n";break;
case '<': printf("F2\n";break;
case '=': printf("F3\n";break;
case '>': printf("F4\n";break;
case '?': printf("F5\n";break;
case '@': printf("F6\n";break;
case 'A': printf("F7\n";break;
case 'B': printf("F8\n";break;
case 'C': printf("F9\n";break;
case 'D': printf("F10\n";break;
case 133: printf("F11\n";break;
case 134: printf("F12\n";break;
case 'R': printf("Ins\n";break;
case 'S': printf("Del\n";break;
case 'G': printf("Home\n";break;
case 'O': printf("End\n";break;
case 'I': printf("PgUp\n";break;
case 'Q': printf("PgDn\n";break;
default:
printf("Unknown\n");
}
}