1

I read that you have to use getch() twice to get a value when an arrow key is pressed. The first call returns 0 for arrow keys and the second call returns another value (eg. 77 for right arrow keys). I wrote the following program to confirm this, but the first value I get is 224, not zero:

#include <stdio.h>  
    int main()  
    {  
        printf("Start: (x to quit)\n");  
        int d = getch();  
        int e = getch();  
        printf("%d", d);  
        printf("\n%d", e);  
    }  

Why is the first value not zero, and what is the significance of 224?

  • There are at least two quite different functions called `getch`. One is specific to Windows, and is (I think) declared in ``. Another is part of the Unix "curses" library. You're probably using the Windows version. If so, please update your question with a "windows" tag to make that clear. – Keith Thompson Dec 13 '15 at 21:48
  • None of the given answers improve on any of the possible duplicates for this question. – Thomas Dickey Dec 13 '15 at 22:34

2 Answers2

1

The "escape" code 0 applies to the numeric keypad (with NumLock off). The dedicated cursor control keys (and Home etc) use the escape code 224. Please try this:

#include <stdio.h>
#include <conio.h>

int main()
{
    int d=-1, e=-1;
    printf("Start:\n");  
    d = _getch();  
    if (d == 0 || d == 224)
       e = _getch();  
    printf("%d %d\n", d, e);
    return 0;
}

Note too that the MSVC function getch() is deprecated, use _getch() (these are non-echoing versions of getche() and _getche()).

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

try this

#include <stdio.h>
#include <conio.h>  
int main()  
{  
    printf("Start: (x to quit)\n");  
    char d = _getche();  
    char e = _getche();  
    printf("%c", d);  
    printf("\n%c", e);  
} 

I didn't try it but i think it will work

fedi
  • 368
  • 3
  • 7
  • 18