This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
programming:ncurses [2022/03/19 08:03] nanodano |
programming:ncurses [2022/03/19 23:56] (current) nanodano |
||
|---|---|---|---|
| Line 95: | Line 95: | ||
| - | ===== Simple examples | + | ===== Examples |
| + | ==== Hello world ==== | ||
| <code cpp hello_curses.cpp> | <code cpp hello_curses.cpp> | ||
| // g++ hello_curses.cpp -lncurses | // g++ hello_curses.cpp -lncurses | ||
| Line 109: | Line 110: | ||
| </ | </ | ||
| - | |||
| - | <code cpp sigkill.cpp> | ||
| - | // Allow CTRL-C to kill app | ||
| - | |||
| - | // Near beginning, link signal to callback | ||
| - | signal(SIGINT, | ||
| - | |||
| - | static void finish(int sig) | ||
| - | { | ||
| - | endwin(); | ||
| - | |||
| - | /* do your non-curses wrapup here */ | ||
| - | |||
| - | exit(0); | ||
| - | } | ||
| - | </ | ||
| <code cpp example.cpp> | <code cpp example.cpp> | ||
| Line 192: | Line 177: | ||
| </ | </ | ||
| + | ==== Shelling out ==== | ||
| <code cpp shell_out.cpp> | <code cpp shell_out.cpp> | ||
| Line 204: | Line 190: | ||
| + | ==== Test mouse input ==== | ||
| + | <code cpp mouse_test.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | void redrawBorder() { | ||
| + | clear(); | ||
| + | box(stdscr, | ||
| + | |||
| + | } | ||
| + | int main() { | ||
| + | |||
| + | initscr(); | ||
| + | mousemask(ALL_MOUSE_EVENTS, | ||
| + | | ||
| + | /** | ||
| + | | ||
| + | */ | ||
| + | if (has_colors()) { start_color(); | ||
| + | // | ||
| + | init_pair(1, | ||
| + | init_pair(2, | ||
| + | | ||
| + | cbreak(); | ||
| + | keypad(stdscr, | ||
| + | nonl(); | ||
| + | noecho(); | ||
| + | curs_set(0); | ||
| + | | ||
| + | attrset(COLOR_PAIR(1)); | ||
| + | redrawBorder(); | ||
| + | // | ||
| + | | ||
| + | | ||
| + | | ||
| + | // short id; /* ID to distinguish multiple devices */ | ||
| + | // int x, y, z; /* event coordinates */ | ||
| + | // mmask_t bstate; | ||
| + | |||
| + | while (true) { | ||
| + | int c = getch(); | ||
| + | switch (c) { | ||
| + | case ' | ||
| + | endwin(); | ||
| + | exit(0); | ||
| + | case KEY_MOUSE: | ||
| + | MEVENT event; | ||
| + | if (getmouse(& | ||
| + | clear(); | ||
| + | redrawBorder(); | ||
| + | |||
| + | attrset(COLOR_PAIR(2)); | ||
| + | mvaddstr(5, | ||
| + | mvaddstr(6, | ||
| + | mvaddstr(7, | ||
| + | addstr(std:: | ||
| + | if (event.bstate & BUTTON1_PRESSED) { | ||
| + | addstr(" | ||
| + | } else if (event.bstate & BUTTON1_DOUBLE_CLICKED) { | ||
| + | addstr(" | ||
| + | } | ||
| + | attrset(COLOR_PAIR(1)); | ||
| + | |||
| + | } | ||
| + | break; | ||
| + | |||
| + | /** | ||
| + | BUTTON1_PRESSED | ||
| + | BUTTON1_RELEASED | ||
| + | BUTTON1_CLICKED | ||
| + | BUTTON1_DOUBLE_CLICKED | ||
| + | BUTTON1_TRIPLE_CLICKED | ||
| + | BUTTON2_PRESSED | ||
| + | BUTTON2_RELEASED | ||
| + | BUTTON2_CLICKED | ||
| + | BUTTON2_DOUBLE_CLICKED | ||
| + | BUTTON2_TRIPLE_CLICKED | ||
| + | BUTTON3_PRESSED | ||
| + | BUTTON3_RELEASED | ||
| + | BUTTON3_CLICKED | ||
| + | BUTTON3_DOUBLE_CLICKED | ||
| + | BUTTON3_TRIPLE_CLICKED | ||
| + | BUTTON4_PRESSED | ||
| + | BUTTON4_RELEASED | ||
| + | BUTTON4_CLICKED | ||
| + | BUTTON4_DOUBLE_CLICKED | ||
| + | BUTTON4_TRIPLE_CLICKED | ||
| + | BUTTON_SHIFT | ||
| + | BUTTON_CTRL | ||
| + | BUTTON_ALT | ||
| + | ALL_MOUSE_EVENTS | ||
| + | REPORT_MOUSE_POSITION | ||
| + | */ | ||
| + | |||
| + | case KEY_RESIZE: | ||
| + | redrawBorder(); | ||
| + | mvaddstr(10, | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | /** | ||
| + | * Cleanup and shutdown | ||
| + | */ | ||
| + | endwin(); | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Modifier keys ==== | ||
| + | |||
| + | <code cpp> | ||
| + | |||
| + | // int c = getch(); | ||
| + | // switch (c) | ||
| + | case ctrl(' | ||
| + | addstr(" | ||
| + | break; | ||
| + | default: | ||
| + | if (c == KEY_ENTER) { | ||
| + | addstr(" | ||
| + | } | ||
| + | if (c == 27) { // Alt key was down when key pressed | ||
| + | // Get the next char that came with the alt | ||
| + | c = getch(); | ||
| + | addstr(" | ||
| + | addch(c); // could be ' | ||
| + | } | ||
| + | |||
| + | </ | ||
| Line 216: | Line 339: | ||
| endwin(); // Cleanup for exit or shell out (refresh will bring state) | endwin(); // Cleanup for exit or shell out (refresh will bring state) | ||
| getmaxyx() // get terminal size | getmaxyx() // get terminal size | ||
| + | |||
| + | cbreak() | ||
| + | nonl() | ||
| + | noecho() | ||
| + | curs_set() | ||
| Line 233: | Line 361: | ||
| keypad(stdscr, | keypad(stdscr, | ||
| + | if (has_colors()) { start_color(); | ||
| use_default_colors() | use_default_colors() | ||
| Line 257: | Line 386: | ||
| - | mousemask() // Specify which mouse events to get | + | mousemask(ALL_MOUSE_EVENTS, |
| + | MEVENT // data type with bits set for specific mouse events like BUTTON1_CLICKED | ||
| // Special events from wgetch() | // Special events from wgetch() | ||