【问题标题】:Can't get KEY_UP with ncurses无法使用 ncurses 获得 KEY_UP
【发布时间】:2016-02-27 17:40:38
【问题描述】:

我正在尝试制作一个简单的程序来检测 UP 键。这是我的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;
}

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

提前致谢。

【问题讨论】:

    标签: c ncurses curses


    【解决方案1】:

    您应该致电keypad 告诉curses 来解释特殊键:

    keypad(stdscr, TRUE);
    

    initscr() 之后。没有它,您的程序将(分别)读取 escape [A

    进一步阅读:

    【讨论】:

    • 它有效!当我按下向上键时,它会打印“向上!”但是当我按下另一个键(包括向上键)时,它就存在了。有什么想法吗?
    • 您的程序只调用了两次getch,并在第二次调用后退出。另外,您可能想致电cbreak
    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2020-12-17
    • 2015-01-16
    • 2012-10-27
    相关资源
    最近更新 更多