无法使用 ncurses 获得 KEY_UP

Can't get KEY_UP with ncurses

我正在尝试制作一个简单的程序来检测向上键。这是我的 C 代码:

#include <stdio.h>
#include <ncurses.h>

int main() {

    initscr();
    noecho();

    printw("hello\n");
    refresh();

    int ch = getch();
    if (ch == KEY_UP) {
        printw("up!\n");
        refresh();
    }

    getch();
    endwin();
    return 0;
}

当我编译并 运行 程序时,它不起作用。当我按下向上键时,它就退出了。

提前致谢。

你应该调用 keypad 来告诉 curses 解释特殊键:

keypad(stdscr, TRUE);

initscr() 之后。否则,您的程序将(分别)读取 escape [A.

进一步阅读: