I wanted to know what integer/ASCII code the arrow keys were so I can get some programming exercise going. My test somehow evolved into this:

int x = getch();
    printf("%c\n", x);
    x = getch();
    printf("%c\n", x);
    x = getch();
    printf("%c\n", x);
    x = getch();
    printf("%c\n", x);
    x = getch();
    printf("%c\n", x);

Problem is the code I'm getting is of a different ASCII character and that there seems to be something being left in the buffer/stream which doesn't happen when I test this using normal characters(Alphabet and such).

Basically it only lets me input three times, skipping the next getch() and printing a constant character first(int 224) then printing a character that supposedly corresponds to the arrow keys(Seeing as this changes along with changing which arrow key I press) but non the less not what I am looking for.

So I have (2) concerns.
(1) I can't determine what ASCII code the arrow keys are.

(2) There seems to be something left on the buffer/stream which doesn't happen ordinarily and skips the next getch(). I'd like to know why this happens.

Wow thanks. I don't understand this part however:

int get_code ( void )
{
  int ch = getch();

  if ( ch == 0 || ch == 224 )
    ch = 256 + getch();

  return ch;
}

I don't understand why that condition on the 'if' is being used, as well as the 256 part.

Not all keystrokes on your keyboard are represented by a single key code. Some keys have both an introduction code and a value code.

For example, the F keys (F1, F2, etc...) typically have 0 as the introduction code and then another value designating each key. Thus you would need to call getch twice to get the full code. Another example is the Ins, Home, Page cluster and dedicated arrow keys. Those typically have an introduction code of 224 and then the value code.

Why is this necessary? First because it's somewhat difficult to handle all possible keys on a keyboard with the limited unsigned char type and second because the value codes are reused. Take the Home, Up Arrow, and Page Up keys. First, notice that you have the dedicated keys in their own cluster, and overloaded keys in the number pad cluster. Home, Up Arrow, and Page Up do double duty as 7, 8, and 9 in the number pad cluster, but the value of those keys are no different from the dedicated keys (eg. 71, 72, and 73). So to tell the difference between those, the full key codes are (0:71), (0:72), and (0:73) for the dedicated keys and (224:71), (224:72), and (224:73) for the number pad keys.

The 256 part just normalizes the disparate code combinations in such a way as to avoid mixing the code up with an unrelated key while making sure that you'll get an arrow key regardless of whether the dedicated keys were used or the number pad was used.

Here's a better loop to fully understand exactly what keys do what:

while (ch != 0x1B)  // exit on ESC
{
    ch = getch();
    printf("%02X ", ch);
}

Watch the output carefully to see what keys use 2 chars.

The 256 part just normalizes the disparate code combinations in such a way as to avoid mixing the code up with an unrelated key while making sure that you'll get an arrow key regardless of whether the dedicated keys were used or the number pad was used.

But any other number as long as I'll always get a number higher than the highest ASCII code will work just as good, no?

But any other number as long as I'll always get a number higher than the highest ASCII code will work just as good, no?

Sure. A robust implementation would store all parts of the scan code for detailed querying. Adding 256 to the latter half is something of a cop-out, but for the most part it works well for the purpose of detecting extended keys.

its complicated dude , i m puzzlied

its complicated dude , i m puzzlied

So do you want clarification, or are you just posting to see your name in the thread?

arrow keys doesn't have the ascii codes. They have the scan codes.
To obtain them we need to write like
char ch;
ch=getch();
ch=getch();
left arrow->75
right arrow->77
up arrow->72
down arrow->80
Happy.......

Perhaps you can use this to help you ...

// attempt  to trap arrow keys
// tested with Code::Blocks 8.02

#include <stdio.h>

static int get_code(void);

// System dependent key codes
enum
{
    KEY_ESC     = 27,
    ARROW_UP    = 256 + 72,
    ARROW_DOWN  = 256 + 80,
    ARROW_LEFT  = 256 + 75,
    ARROW_RIGHT = 256 + 77
};

int main(void)
{
    int ch;

    puts("Press arrow keys, escape key + enter to exit:");
    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;
        }
    }
    getchar();   // wait
    return 0;
}

static int get_code(void)
{
    int ch = getch();

    if (ch == 0 || ch == 224)
        ch = 256 + getch();

    return ch;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.