Install and use the curses library in Linux

Source: Internet
Author: User
The following content is transferred fromHttp://blog.163.com/lghct@126/blog/static/64551808201024101630455/

The curses library is used to develop a simple graphic interface program library (maybe it has more than this function, but currently I only know this one! :).

Ubuntu installation method:

Sudo apt-Get install libncurses5-dev

The basic usage of curses is as follows:

1. include header file: Curses. h

2. When compiling, add the link statement-lcurses, for example, GCC temp. C-o temp-lcurses.

3. Important functions:

Initscr (): Before you start curses programming, you must use the initscr () function to enable the curses mode.

Endwin (): The last function called when curses programming is completed.

Move (Y, X): move the cursor to the position of X and Y.
Getyx (Win, Y, X): Get the current cursor position. (Please note! Is y, X instead of & Y, & X)
Clear () and erase (): Clear the entire screen. (use refresh () with caution)
Echochar (CH): displays a character.
Addch (CH): displays a character.
Mvaddch (Y, X, CH): displays a character on (x, y). It is equivalent to calling move (Y, x); addch (CH );
Addstr (STR): displays a string.
Mvaddstr (Y, X, STR): displays a string on (x, y). It is equivalent to calling move (Y, x); addstr (STR );
Printw (format, STR): similar to printf (), output to the screen in a certain format.
Mvprintw (Y, X, format, STR): printw at the (x, y) position. equivalent to calling move (Y, x); printw (format, STR );
Getch (): Read a character from the keyboard. (note! Returns an integer)
Getstr (): reads a string of characters from the keyboard.
Scanw (format, & arg1, & arg2. ..): reads a string of characters from the keyboard, just like scanf.
Beep (): a beep.
Box (Win, win, CH2): automatically draws a box

Set of common initialization functions:

Void initial ()
{
Initscr (); // enable the curses Mode
Cbreak (); // enable the cbreak mode. All input characters except delete or Ctrl are read one by one.
Nonl (); // used to determine whether the return key is mapped to a newline character when data is input.
Noecho (); // echo () and noecho (): This function is used to control whether a character is displayed on the terminal when a token is input from the keyboard.
Intrflush (stdscr, false );
Keypad (stdscr, true); // when keypad is enabled, you can use some special characters on the keyboard, such as Up, down, left, right, and other direction keys.
Refresh (); // clears the screen
}

These are some simple applications of the curses library. Now we must learn what we have learned. If you learn how much you use, you must learn it once you have learned it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.