Search code examples
ncursescurses

wgetch is not detecting KEY_UP etc


I have enabled keypad for my_win and yet when I press KEY_UP after running the program, nothing happens. The stranger thing is that it works completely fine with stdscr. It appears to only be an issue with my_win.

/* Selects different elements of the list */
    while ((ch = wgetch(my_win)) != 'q')
    {
        sprintf(item, "%-12s", list[i]);
        mvwprintw(my_win, i + 1, 2, "%s", item);

        switch(ch)
        {
            case KEY_LEFT:
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,--startx);
                wprintw(my_win, "This is some text");
                wrefresh(my_win);
                break;
            case 'd':
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,++startx);
                wprintw(my_win, "This is some text");
                wrefresh(my_win);
                break;
            case 'w':
                destroy_win(my_win);
                my_win = create_newwin(height, width, --starty,startx);
                wprintw(my_win, "This is some text");
                wrefresh(my_win);
                break;
            case 's':
                destroy_win(my_win);
                my_win = create_newwin(height, width, ++starty,startx);
                wprintw(my_win, "This is some text");
                wrefresh(my_win);
                break;  
            case KEY_UP:
                i--;
                i = (i < 0) ? 4 : i;
                break;
            case KEY_DOWN:
                i++;
                i = (i < 0) ? 4 : i;
                break;
        }

Solution

  • The keypad function applies only to the window given as its parameter. When you use newwin, etc., those return a window where the keypad mode is not set.

    The newwin documentation points out

    Regardless of the function used for creating a new window (e.g., newwin, subwin, derwin, newpad), rather than a duplicate (with dupwin), all of the window modes are initialized to the default values. These functions set window modes after a window is created:

    idcok, idlok, immedok, keypad, leaveok, nodelay, scrollok,
    setscrreg, syncok, wbkgdset, wbkgrndset, and wtimeout