PersianGulf
PersianGulf

Reputation: 2935

getch() of ncurses doesn't work

I need to create a mainloop for my program and wrote the following function:

void menu(){
int ch;
cbreak();
noecho();
initscr();
refresh();
while (ch != KEY_F(9)){
    ch = getch();
    cout << ch << endl;
    switch (ch){
        case KEY_F(1): add();
            break;

        case KEY_F(2): edit();
            break;

        case KEY_F(3):
            break;

        case KEY_F(4):
            break;


    }

}

endwin();

}

But getch() doesn't work and print -1 in loop. how i do? May i set special attr or call special func?

Upvotes: 0

Views: 6039

Answers (3)

PersianGulf
PersianGulf

Reputation: 2935

Yes, my loop is here:

initscr();
clear();
noecho();
cbreak();   /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;


menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 0, "Name of my program");
refresh();
print_menu(menu_win, highlight);
while (true)

    {   c = wgetch(menu_win);
        switch(c){
TYPE OF KEYS;
}//END OF SWITCH
}//END OF LOOP

Upvotes: 0

Lubulos
Lubulos

Reputation: 290

First, every ncurses function should be used only after initscr() has been called. In your code cbreak() and noecho() are probably ignored.
Second, if you want to use function keys, you have to tell that to ncurses, by calling keypad(stdscr, TRUE). However, since not every computer has got function keys, you should always check if the system support that functionality, using has_key() (same for has_colors() that checks if current terminal supports colors).

Upvotes: 0

Duck
Duck

Reputation: 27542

You need to call keypad() e.g. keypad(stdscr, TRUE). But beyond that the function keys may not work on your terminal. Check your ncurses.h file for a has_key() function and you can use that to determine if they are available on your terminal.

Upvotes: 1

Related Questions