Ncurses键盘输入

问题描述 投票:0回答:1

我测试了getchgetchar,但是它正在等待输入,我认为必须有一个读取键盘缓冲区的函数。我的代码的一部分

while (1) {
    if (key!='r')
    {
        if (key!='q')
        { 
            mvprintw(LINES-2, 1, "Display will refresh in %2d seconds", t);
            refresh();  
            sleep(1);
            t--;
            break;
        }
        else
        {
        exit (0);
        }
    }
    else
    {
    return;
    }
}
c linux input keyboard ncurses
1个回答
4
投票

如果不想让getch()等待,则必须使用nodelay()将其设置为非阻塞。

执行后:

if (nodelay (pWin, 1) == ERR) {
    // some error occurred.
}

然后,如果没有可用的输入,则getch()将返回ERR

输入选项的联机帮助页为here,并且在此链接及其链接中也提及了getch的行为,链接here


nodelay选项使getch成为非阻塞调用。如果没有准备好输入,getch将返回ERR。如果被禁用(bf为FALSE),getch将等待直到按下某个键。


在无延迟模式下,如果没有输入在等待,则返回值ERR。

© www.soinside.com 2019 - 2024. All rights reserved.