Search code examples
cncursescurses

Can't get KEY_UP with ncurses


i am trying to make a simple program to detect the UP key. here is my c code:

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

when i compile and run the program, it doesn't work. when i press up key it just exits.

thanks in advance.


Solution

  • You should call keypad to tell curses to interpret special keys:

    keypad(stdscr, TRUE);
    

    after initscr(). Without that, your program will read (separately) escape [ and A.

    Further reading: