输入时光标离开 window (ncurses)
Cursor leaves window when typing (ncurses)
我正在使用 ncurses 为我的应用程序制作一个简单的 TUI。
我掌握了创建和打印到 window 的基础知识,但我在输入方面遇到问题。
当我写完的时候,光标定位在我写的字符串的末尾
但是当我开始输入时,光标移动到终端的左上角 window。
如何在打字时将其固定到位?
这是我的代码:
#include <ncurses.h>
WINDOW *win;
int startx, starty, width, height;
int cport;
WINDOW *makewin(int h, int w, int y, int x)
{
WINDOW *lwin;
lwin = newwin(h, w, y, x);
box(lwin, 0 , 0);
wrefresh(lwin);
return lwin;
}
void dewin(WINDOW *lwin)
{
wborder(lwin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(lwin);
delwin(lwin);
}
void getPort(){
win = makewin(height, width, starty, startx);
wbkgd(win, COLOR_PAIR(1));
mvwprintw(win, 0, 8, "Port Settings");
mvwprintw(win, 2, 4, "Set port server should");
mvwprintw(win, 3, 4, "listen to: ");
wrefresh(win);
scanw("%d", &cport);
}
int main()
{
initscr();
cbreak();
keypad(stdscr, TRUE);
start_color();
init_pair(1,COLOR_WHITE, COLOR_BLACK);
init_pair(2,COLOR_WHITE, COLOR_BLUE);
bkgd(COLOR_PAIR(2));
refresh();
height = 6;
width = 30;
starty = (LINES - height) / 2;
startx = (COLS - width) / 2;
getPort();
getch();
dewin(win);
endwin();
return 0;
}
scanw
(and wscanw
) ultimately call wgetch
,刷新作为其参数给出的 window:
If the window is not a pad, and it has been moved or modified since the last call to wrefresh
, wrefresh
will be
called before another character is read.
也就是说,stdscr
的任何未决更改(包括由于 initscr
导致的擦除)将由普通 scanw
应用。光标将留在 window 程序要求输入的当前位置。
这是因为您使用的是 scanw(...)
函数,它最终的作用类似于 wscanw(stdscr, ...)
,它从 stdscr
获取输入。使用wscanw
函数解决问题。
我正在使用 ncurses 为我的应用程序制作一个简单的 TUI。 我掌握了创建和打印到 window 的基础知识,但我在输入方面遇到问题。
当我写完的时候,光标定位在我写的字符串的末尾
但是当我开始输入时,光标移动到终端的左上角 window。
如何在打字时将其固定到位?
这是我的代码:
#include <ncurses.h>
WINDOW *win;
int startx, starty, width, height;
int cport;
WINDOW *makewin(int h, int w, int y, int x)
{
WINDOW *lwin;
lwin = newwin(h, w, y, x);
box(lwin, 0 , 0);
wrefresh(lwin);
return lwin;
}
void dewin(WINDOW *lwin)
{
wborder(lwin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(lwin);
delwin(lwin);
}
void getPort(){
win = makewin(height, width, starty, startx);
wbkgd(win, COLOR_PAIR(1));
mvwprintw(win, 0, 8, "Port Settings");
mvwprintw(win, 2, 4, "Set port server should");
mvwprintw(win, 3, 4, "listen to: ");
wrefresh(win);
scanw("%d", &cport);
}
int main()
{
initscr();
cbreak();
keypad(stdscr, TRUE);
start_color();
init_pair(1,COLOR_WHITE, COLOR_BLACK);
init_pair(2,COLOR_WHITE, COLOR_BLUE);
bkgd(COLOR_PAIR(2));
refresh();
height = 6;
width = 30;
starty = (LINES - height) / 2;
startx = (COLS - width) / 2;
getPort();
getch();
dewin(win);
endwin();
return 0;
}
scanw
(and wscanw
) ultimately call wgetch
,刷新作为其参数给出的 window:
If the window is not a pad, and it has been moved or modified since the last call to
wrefresh
,wrefresh
will be called before another character is read.
也就是说,stdscr
的任何未决更改(包括由于 initscr
导致的擦除)将由普通 scanw
应用。光标将留在 window 程序要求输入的当前位置。
这是因为您使用的是 scanw(...)
函数,它最终的作用类似于 wscanw(stdscr, ...)
,它从 stdscr
获取输入。使用wscanw
函数解决问题。