简体   繁体   中英

Getch() behavior when terminal is resized? (ncurses)

getch returns 410 when I resize terminal. Why does the getch interpret the resize as input? What's the 410? Is it a special symbol?

Code:

#include <curses.h>

int main() {
    initscr(); cbreak(); noecho();
    int x = getch();
    printw("%d", x);
    getch();
    endwin();

    return 0;
}

But when I use timeout I get ERR (ie -1). Why does resizing when using the timeout lead to an error?

Code:

#include <curses.h>

int main() {
    initscr(); cbreak(); noecho(); timeout(10000);
    int x = getch();
    printw("%d", x);
    getch();
    endwin();

    return 0;

}
/* curses.h */
#define KEY_RESIZE  0632        /* Terminal resize event */

410 = 0632 (octal).

As for your second question, that doesn't happen when I test on my machine. Are you perhaps running an old version of ncurses? There have been bugs around this. https://lists.gnu.org/archive/html/bug-ncurses/2002-01/msg00003.html

When using timeout (in this case, 10 seconds), getch will return ERR when the timeout expires. That's in the manual pages:

The timeout and wtimeout routines set blocking or non-blocking read for a given window. If delay is negative, blocking read is used (ie, waits indefinitely for input). If delay is zero, then non-blocking read is used (ie, read returns ERR if no input is waiting). If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input. Hence, these routines provide the same functionality as nodelay , plus the additional capability of being able to block for only delay milliseconds (where delay is positive).

and

returns ERR if the window pointer is null, or if its timeout expires without having any data, or if the execution was interrupted by a signal (errno will be set to EINTR ).

If you're patient (or testing slowly), you'll see a -1 returned. It's easier if you make the program wait:

#include <curses.h>

int main() {
    initscr(); cbreak(); noecho(); timeout(10000);
    for (;;) {
    int x = getch();
    move(0,0);
    printw("%d", x);
    getch();
    clrtobot();
    }
    endwin();

    return 0;

}

That's the usual case. If you're seeing something different, then the relevant information (version of ncurses and the platform) should be part of the question.

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