-1

I am trying to create a program that response to keyboard input. At the moment the menu below functions correctly but upon pressing enter I am running into issues. When I press enter nothing happens. I am just wondering why this is happening? Many thanks!

#include <ncurses.h>

#define MENUMAX 6

void drawmenu(int item)
{
        int c;
        char mainmenu[] = "Menu";
        char menu[MENUMAX] [10] = {
                "1",
                "2",
                "3",
                "4",
                "5",
                "6"
        };

        clear();
        attron(A_BOLD | A_UNDERLINE);
        addstr(mainmenu);
        attroff(A_BOLD | A_UNDERLINE);
        for( c = 0; c < MENUMAX; c++)
        {
                if( c == item)
                        attron(A_REVERSE);
                attron(A_BOLD);
                mvaddstr(3 + (c * 2), 20, menu[c]);
                attroff(A_BOLD);
                attroff(A_REVERSE);
        }
        refresh();
}

int main(int argc, char *argv[])
{
        int key, menuitem;
        menuitem = 0;
        initscr();
        drawmenu(menuitem);
        keypad(stdscr, TRUE);
        noecho();
        do
        {
                raw();
                nonl();
                key = getch();
                switch(key)
                {
                        case KEY_DOWN:
                                menuitem++;
                                if(menuitem > MENUMAX - 1) menuitem = 0;
                                break;
                        case KEY_UP:
                                menuitem--;
                                if(menuitem < 0) menuitem = MENUMAX - 1;
                                break;
                        case KEY_ENTER:
                                mvaddstr(17, 25, "Test mesage!");
                                refresh();
                                break;
                        default:
                                break;
                }
                drawmenu(menuitem);
        } while(key != '~');

        echo(); 


        endwin();
        return 0;
}
NSPredator
  • 464
  • 4
  • 10

1 Answers1

0

The program has not provided for character-at-a-time (unbuffered) input, as described in the Initialization section of the ncurses manual page. Since it looks for special keys such as KEY_UP, that means it should use cbreak (rather than raw, which prevents ncurses from decoding special keys).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • So am I replacing `break;` with `cbreak;` ? – NSPredator Oct 05 '15 at 13:58
  • `cbreak` (see the manpage links in my answer) is a function. It is named for one of the common terminal modes. [This page](https://utcc.utoronto.ca/~cks/space/blog/unix/CBreakAndRaw) appears to provide a useful introduction to the terminology. – Thomas Dickey Oct 05 '15 at 21:52