How do you get a single character in C without pressing the return key?
Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
/* System dependent key codes */
enum
{
KEY_ESC = 27,
ARROW_UP = 256 + 72,
ARROW_DOWN = 256 + 80,
ARROW_LEFT = 256 + 75,
ARROW_RIGHT = 256 + 77
};
static int get_code ( void )
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
int main ( void )
{
int ch;
while ( ( ch = get_code() ) != KEY_ESC ) {
switch ( ch ) {
case ARROW_UP:
printf ( "UP\n" );
break;
case ARROW_DOWN:
printf ( "DOWN\n" );
break;
case ARROW_LEFT:
printf ( "LEFT\n" );
break;
case ARROW_RIGHT:
printf ( "RIGHT\n" );
break;
}
}
return 0;
}
I have this code that bitmaps for arrow keys, but I'm not sure how to do this with even more characters. I understand the switch statement and know what all the special keywords/statements do in this code. I'm just confused on how exactly one finds out how to map this, or if I could just somehow translate extended ASCII sets/ regular characters ( e.g. 65 = 'A' ). Also I know usually regular ASCII ends at 255, so what is with the 256( why does it get added to the getch() function and why is it that number)? Also, is this a good way of doing it, or should I use a more standard way?