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