getch and arrow codes

arrow-keys, c, character, decimal, getch

Solution

By pressing one arrow key `getch` will push three values into the buffer:

- `'\033'`

- `'['`

- `'A'`, `'B'`, `'C'` or `'D'`

So the code will be something like this:

if (getch() == '\033') { // if the first value is esc
    getch(); // skip the [
    switch(getch()) { // the real value
        case 'A':
            // code for arrow up
            break;
        case 'B':
            // code for arrow down
            break;
        case 'C':
            // code for arrow right
            break;
        case 'D':
            // code for arrow left
            break;
    }
}

Problem

I'm writing a programm that's using `getch()` to scan for arrow keys. My code so far is: ``` switch(getch()) { case 65: // key up break; case 66: // key down break; case 67: // key right break; case 68: // key left break; } ``` Problem is that when I press `'A'`, `'B'`, `'C'` or `'D'` the code will also executed, because `65` is the decimal code for `'A'`, etc... Is there a way to check for an arrow key without call others? Thanks!

Original source

Related problems