Let's write a program which will clarify the usage of these functions.
Example 2. Initialization Function Usage example
/* File path: basics/init_func_example.c */ #include <ncurses.h> int main() { int ch; initscr(); /* Start curses mode */ raw(); /* Line buffering disabled */ keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ noecho(); /* Don't echo() while we do getch */ ch = getch(); /* If raw() hadn't been called * we have to press enter before it * gets to the program */ if(ch == KEY_F(1)) /* Without keypad enabled this will */ printw("F1 Key pressed");/* not get to us either */ /* Without noecho() some ugly escape * characters might have been printed * on screen */ else { printw("The pressed key is "); attron(A_BOLD); printw("%c", ch); attroff(A_BOLD); } refresh(); /* Print it on to the real screen */ endwin(); /* End curses mode */ return 0; } |
This program is self-explanatory. But I used functions which aren't explained yet. The function getch() is used to get a character from user. It is equivalent to normal getchar() except that we can disable the line buffering to avoid <enter> after input. Look for more about getch()and reading keys in the key management section . The functions attron and attroff are used to switch some attributes on and off respectively. In the example I used them to print the character in bold. These functions are explained in detail later.