C pointer principle (59)-Ncurses-text terminal graphics

Source: Internet
Author: User

Keyboard Management

Let's create a simple single-Screen Editor.

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

First, construct an editor with only the input function, and use wgetch to capture the input.

# Include

# Include

# Include

Int main (int argc, char * argv [])

{

Setlocale (LC_ALL ,"");

Initscr ();

Clear ();

Noecho ();

Cbreak ();

If (has_colors () = FALSE)

{

Endwin ();

Printf ("your terminal does not support color! \ N ");

Return (1 );

}

Start_color ();/* Start the color mechanism */

Init_pair (1, COLOR_GREEN, COLOR_BLACK );

WINDOW * win1;

Int width = COLS-14;

Int height = LINES-14;

Int x, y;

Win1 = newwin (height, width, 7,7); // new window (row, column, begin_y, begin_x)

Keypad (win1, TRUE );

Box (win1, ACS_VLINE, ACS_HLINE );

Wattron (win1, COLOR_PAIR (1 ));

Wrefresh (win1 );

Getyx (win1, y, x );

++ Y; ++ x;

While (1 ){

Int c = mvwgetch (win1, y, x );

++ X;

If (x> = width-1 ){

++ Y;

X = 1;

}

If (y> = height-1 ){

Y = 1;

}

Mvwprintw (win1, y, x, "% c", c );

Wrefresh (win1 );

}

Wattroff (win1, COLOR_PAIR (1 ));

Endwin ();

Return 0;

}

Run

Dp @ dp :~ /Cursestest % gcc-lncursesw a. c-o mytest

Dp @ dp :~ /Cursestest %./mytest

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/


Vc/yvPy1xNans9ajrNLGtq + 3vc/yvPyjrL/mixer + ezwvcD4KPHA + c2V0bG9jYWxlKExDX0FMTCw = "");

Initscr ();

Clear ();

Noecho ();

Cbreak ();

If (has_colors () = FALSE)

{

Endwin ();

Printf ("your terminal does not support color! \ N ");

Return (1 );

}

Start_color ();/* Start the color mechanism */

Mvprintw (5, COLS/2-10, "simple Editor-limited to single screen editing ");

Refresh ();

Init_pair (1, COLOR_GREEN, COLOR_BLACK );

WINDOW * win1;

Int width = COLS-14;

Int height = LINES-14;

Int x, y;

Win1 = newwin (height, width, 7,7); // new window (row, column, begin_y, begin_x)

Keypad (win1, TRUE );

Box (win1, ACS_VLINE, ACS_HLINE );

Wattron (win1, COLOR_PAIR (1 ));

Wrefresh (win1 );

Getyx (win1, y, x );

++ Y; ++ x;

While (1 ){

Int c = mvwgetch (win1, y, x );

Switch (c)

{

Case KEY_RIGHT:

++ X;

If (x> = width-1 ){

++ Y;

X = 1;

}

Break;

Case KEY_LEFT:

-- X;

If (x <1 ){

-- Y;

X = width-2;

}

Break;

Case KEY_UP:

-- Y;

If (y <1 ){

Y = height-2;

}

Break;

Case KEY_DOWN:

++ Y;

If (y> = height-1 ){

Y = 1;

}

Break;

Default:

Mvwprintw (win1, y, x, "% c", c );

++ X;

If (x> = width-1 ){

++ Y;

X = 1;

}

If (y> = height-1 ){

Y = 1;

}

Wrefresh (win1 );

}

}

Wattroff (win1, COLOR_PAIR (1 ));

Endwin ();

Return 0;

}

Dp @ dp :~ /Cursestest % gcc-lncursesw a. c-o mytest

Dp @ dp :~ /Cursestest %./mytest




Related Article

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.