箭头键 return 与 getch() 对应的值是什么?

What values do arrow keys return with getch()?

我读到您必须使用 getch() 两次才能在按下箭头键时获取值。第一次调用 returns 0 表示方向键,第二次调用 returns 另一个值(例如 77 表示右箭头键)。我编写了以下程序来确认这一点,但我得到的第一个值是 224,而不是零:

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

为什么第一个值不为零,224有什么意义?

试试这个

#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);  
} 

我没试过,但我认为它会起作用

"escape" 代码 0 适用于数字键盘(NumLock 关闭)。专用光标控制键(和 Home 等)使用转义码 224。请试试这个:

#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;
}

另请注意,MSVC 函数 getch() 已弃用,请使用 _getch()(这些是 getche()_getche() 的非回显版本)。