调整终端大小时的 Getch() 行为? (诅咒)

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

当我调整终端大小时获取 returns 410。为什么 getch 将调整大小解释为输入?什么是410?是特殊符号吗?

代码:

#include <curses.h>

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

    return 0;
}

但是当我使用超时时,我得到 ERR(即 -1)。为什么使用超时调整大小会出错?

代码:

#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(八进制)。

关于你的第二个问题,我在我的机器上测试时没有出现这种情况。您可能 运行 是旧版本的 ncurses 吗?这方面存在错误。 https://lists.gnu.org/archive/html/bug-ncurses/2002-01/msg00003.html

当使用timeout (in this case, 10 seconds), getch时,超时时间到时会returnERR。那在手册页中:

The timeout and wtimeout routines set blocking or non-blocking read for a given window. If delay is negative, blocking read is used (i.e., waits indefinitely for input). If delay is zero, then non-blocking read is used (i.e., 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).

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).

如果您有耐心(或测试缓慢),您会看到 -1 returned。如果让程序等待会更容易:

#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;

}

这是通常的情况。如果您看到不同的东西,那么相关信息(ncurses 的版本和平台)应该是问题的一部分。