简体   繁体   中英

ncurses: wgetch fails to read correctly after moving / resizing window

I am having a hard time trying to get wgetch to read data from a window after moving and resizing it.

Upon input, I move the window up and increase it's height by 1 too. I then clear the window and write data back to it. The problem is when I do wgetch (or mvwgetch ) it positions the input cursor at the previous position before I moved the window up.

Here's my code:

#include <ncurses.h>

int main() {
    WINDOW *win=initscr();
    int y,x,i=1;
    getmaxyx(win, y, x);

    //creates a sub windows 1 col high
    WINDOW *child=derwin(win, i, x, y-i, 0); 

    //doc says to touch before refresh
    touchwin(win);

    //print to child
    waddstr(child, "hello");
    wrefresh(child);
    wrefresh(win);

    noecho();
    while(wgetch(child)!='q') {
            ++i;
            mvderwin(child, y-i, 0);
            wresize(child, i, x);
            touchwin(win);

            wclear(child);

            waddstr(child,"hello");
            wrefresh(child);
            wrefresh(win);
    }
    delwin(child);
    delwin(win);
    endwin();
}

Here the word "hello" does move up as expected, however, the input cursor is in the wrong place. Using mvwgetch still causes the same problem. cbreak() , noecho() and scrollok(child) also don't seem to be helping.

Thanks

EDIT: updated version better displaying the problem http://liveworkspace.org/code/31DruQ $0

You have to catch SIGWINCH, that signal is sent when you resize the terminal. Do an endwin(), a refresh(), and then repaint your windows. Cursor position is relative to the windows, not the actual terminal size. The windows are not resized automatically.

Edit: Right, you're actually resizing the windows, not the terminal. In that case, first of all, do a wrefresh on the child LAST, the cursor shown on the screen is the one of the refresh that happened last.

Put a box around your subwindows and check that they're actually getting resized / moved properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM