A simple key usage example
Adapted from NCURSES Programming HOWTO, by Pradeep Padala.
Let’s write a small program which creates a menu that can be navigated with the up and down arrows.
Example: A Simple Key Usage example
#include <stdlib.h>#include <curses.h>
#define WIDTH 30#define HEIGHT 10
int startx = 0;int starty = 0;
const char *choices[] = { "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit",};int n_choices = sizeof(choices) / sizeof(char *);
static void{ int x, y, i;
x = 2; y = 2; for (i = 0; i < n_choices; ++i) { if (highlight == i + 1) /* Highlight the present choice */ { } else ++y; }}
intmain(void){ int highlight = 1; int choice = 0; int c;
* editing control characters */
"Press enter to select a choice"); print_menu(menu_win, highlight); while (1) { switch (c) { if (highlight == 1) highlight = n_choices; else --highlight; break; if (highlight == n_choices) highlight = 1; else ++highlight; break; case 10: choice = highlight; break; default: "Character pressed is = %3d " "Hopefully it can be printed as '%c'", c, c); break; } print_menu(menu_win, highlight); if (choice != 0) /* User made a choice; leave the loop */ break; } choice, choices[choice - 1]); return EXIT_SUCCESS;}Use the up and down arrows to move the highlight, and press Enter to choose. Any other key, F1 included, shows its code on the bottom line, so this program ends only when you choose. The program compares the key with 10, a newline, because ncurses turns the carriage return that Enter sends into a newline while nl() is in effect, which is the default.
The window is centered with LINES and COLS, so the menu stays in the middle at any terminal size.
The program prints your choice just before endwin(). A terminal that draws curses programs on an alternate screen, as xterm and the terminal on this page do, switches back to the normal screen at endwin(), so on xterm the message disappears with the menu. The terminal on this page keeps the program’s last screen in view after it ends, so you can read the choice there.