curses library: why does getch() clear my screen?

我们两清 提交于 2019-12-05 06:50:57

Your first instinct was correct: getch() does an implicit refresh(). Specifically, getch() is equivalent to wgetch(stdscr), so it's an implicit wrefresh(stdscr) -- updating a window (stdscr) that you're not otherwise using, that just happens to fill the screen. The reason that subsequent calls have no effect from that point on, is that stdscr is already up to date, as far as curses is concerned, since you never write to it after that (never mind that its contents have been overwritten on the actual screen).

The solution is either to call refresh() explicitly at the top, before you begin drawing; or, my preference, to call wgetch() on a different window (whichever is most appropriate), instead of getch(), and ignore the existence of stdscr entirely. Just remember that all the functions that don't let you specify a window -- getch(), refresh(), etc. -- are really calls to their "w" equivalents, with stdscr as the implicit window parameter.

By default getch() blocks until a key is pressed. Change your loop to be a do {} while(); loop:

pressed_key = /* some value that will be benign or indicate that nothing has been pressed */

do {
     /* process keys to move the @ cursor (left out because irrelevant) */

     box(wmap, 0 , 0);
     box(wlog, 0 , 0);
     box(wlegend, 0 , 0);
     wrefresh(wmap);
     wrefresh(wlog);
     wrefresh(wlegend);
} while ((pressed_key = getch()) != KEY_F(1));

If you need getch() to be non-blocking, the way to do that is to set curses for nodelay mode on the default window.

From the pdcurses docs:

With the getch(), wgetch(), mvgetch(), and mvwgetch() functions, a character is read from the terminal associated with the window. In nodelay mode, if there is no input waiting, the value ERR is returned. In delay mode, the program will hang until the system passes text through to the program.

So call:

nodelay(stdscr, TRUE);

if you want getch() to be non-blocking; it will return ERR if no key has been pressed.

getch() doesn't clears your screen, it just does what it was made to do, blocking your while loop, waiting to get a character from your keyboard.

So here's something that could fix your problem. Before curses.h, include conio.h, and make your while loop like this:

do
{
box(wmap, 0 , 0);
 box(wlog, 0 , 0);
 box(wlegend, 0 , 0);
 wrefresh(wmap);
 wrefresh(wlog);
 wrefresh(wlegend);
 if(kbhit())
    pressed_key = getch();
}while (pressed_key  != KEY_F(1));

Here's another fix for you, that will also make @Kaz happy. This time we'll use windows.h instead of conio.h, and you don't need that pressed_key anymore. Make your while loop like this:

do
{
     /* process keys to move the @ cursor (left out because irrelevant) */
     box(wmap, 0 , 0);
     box(wlog, 0 , 0);
     box(wlegend, 0 , 0);
     wrefresh(wmap);
     wrefresh(wlog);
     wrefresh(wlegend);

}
while (!GetAsyncKeyState(VK_F1));

By the way using nodelay, as suggested in another answer will fix the current problem, but it will pretty much make the use of curses.h "useless", except for the fact you can still do some quick graphics in the console, that can be made with a bit of skill without the use of any library. You'll see what I mean if you'll make a little animation in that menu, like a moving cursor driven by your keyboard and so on. Basically curses are mostly used just because of its delaying capabilities, making things look more natural in the console, so they won't flicker, especially when it comes to details / animations generated through repetitive loops..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!