1 / 14

CSE 20232 Lecture 11 – Ncurses I/O

CSE 20232 Lecture 11 – Ncurses I/O. Ncurses I/O text-based GUI Simple Program Setting Up & Shutting Down Moving cursor & handling I/O Color (only on some terminals) Special Keys The Mouse. Curses Library. Allows “text-based” GUI Curses or Ncurses for Unix/Linux/AIX

oriel
Download Presentation

CSE 20232 Lecture 11 – Ncurses I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 20232Lecture 11 – Ncurses I/O • Ncurses I/O • text-based GUI • Simple Program • Setting Up & Shutting Down • Moving cursor & handling I/O • Color (only on some terminals) • Special Keys • The Mouse

  2. Curses Library • Allows “text-based” GUI • Curses or Ncurses for Unix/Linux/AIX • Defined in “curses.h” header file • PDCurses (Public Domain Curses) for Dos/Windows • Supports mouse, keypads, color, buffered & non-buffered input, and some simple graphics (lines, boxes, etc)

  3. Curses (Windows, etc) • Curses works by creating a window / screen / console data structure within which the program moves the cursor and performs output • A refresh() function updates the actual window / screen / console to reflect the logical one • See class web site for URLs of Curses resources / tutorials / examples/ etc.

  4. Curses Basics – Hello World! #include<curses.h> int main() { // always do this first initscr(); // initialize curses // these are optional cbreak(); // input keys immediately available noecho(); // stop echoing inputs nonl(); // faster cursor movement clear(); // clear the screen // move cursor to location and output message // upper left screen corner is 0,0 move(12,5); // move cursor to row 12, column 5 printw(“Hello World\n”); // instead of cout << ... refresh(); // make it visible to us getch(); // wait for a key to be hit endwin(); // release window back to shell return 0; }

  5. CursesInitialization & Shutdown • Must use … • initscr();// first, to initialize curses • endwin();// last, to return screen to normal • Probably use … • cbreak();// enable direct keystroke access • noecho();// prevent echoing of input • nonl();// speeds cursor movement • getmaxyx(stdscr,h,w);// get screen limits • Notes: • stdscris the default screen/window

  6. CursesMoving cursor & I/O • Cursor & position • move(y,x);// move cursor to line y, char posit x • Upper left of screen is at position 0,0 • getyx(y,x);// returns current cursor location • I/O • printw(“%d %s”, num, str);// cout << • Parameters are same as printf() • Prints in window from current cursor location • scanw(“%f”,&realNum);// cin >> • Parameters are same as scanf() • Reads input stream as directed by format string • getch();// reads single keystroke • wgetch(stdscr);// reads single keystroke • From indicated window

  7. CursesMoving cursor & I/O • Combined movement and I/O • mvprintw(y,x,“%d %s”, num, str); • Prints in window from cursor location y, x • mvscanw(y,x,“%f”,&realNum); • Reads input stream as directed by format string • Making it visible • refresh();// updates console • Makes it match logical window data structure

  8. Curses Color • Only supported on certain terminal types • xterm is good bet • set term=xterm • at unix prompt before running program • start_color();// initializes color • init_pair(num,fg_color,bg_color); • Creates a COLOR_PAIR of foreground and background colors • xterm has 8 colors and 64 color pairs, max • attron(COLOR_PAIR(num)); • makes color pair the output attribute until turned off • attroff(COLOR_PAIR(num));

  9. Curses COLORS • COLOR_BLACK • COLOR_RED • COLOR_GREEN • COLOR_YELLOW • COLOR_BLUE • COLOR_MAGENTA • COLOR_CYAN • COLOR_WHITE

  10. Curses Using Special Keys • To use function keys, arrows, etc. Initialize with • keypad(stdscr,TRUE); • Must be used before both special key input • And before mouse event capture • prevents immediate translation to ASCII • Get key with • int key = getch(); • Test key with keycodes • KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT • KEY_HOME, KEY_BACKSPACE • KEY_F(n) Function keys, for 0 <= n >= 63 • KEY_DC Delete character • KEY_IC Insert char or enter insert mode • KEY_ENTER Enter or send

  11. Curses Mouse Support • Only in Ncurses (Linux & AIX) • Initialize for capture of events with • mousemask(ALL_MOUSE_EVENTS,NULL); • Specific masks (BUTTON1_CLICKED, ...) • Read mouse click with • int key = getch(); or • int key =wgetch(stdscr);

  12. Curses Mouse Events • Processing event • Value read will equal KEY_MOUSE • Use getmouse(&event) to recover event from event queue • Use event.bstate to determine button state (which event) • Use event.x and event.y to determine where mouse was clicked in window MEVENT event;// data type for mouse event if (key == KEY_MOUSE)// if it was the mouse if (getmouse(&event) == OK) { // get the event if (event.bstate & BUTTON1_CLICKED) { // so do something with // event.y; and event.x;

  13. Example Curses Programs(on class web site) • hi.cpp • Simple curses “Hello World” program • bigHi.cpp • Shows big “HI” on screen • hw4_sample.cpp • Shows size of curses window, once open • mvbox.cpp • Moving star shape: Uses keyboard (letter) inputs • mvbox2.cpp • Moving star and box shapes: Uses keyboard (letter) inputs • Mvbox3.cpp • Like mvbox2.cpp but also uses arrow keys and mouse • tttCM (executable only) • TicTacToe: Uses Color, mouse and keyboard inputs

  14. Finis • Look at some sample Ncurses Programs • Answer questions on any topic • Adios

More Related