【问题标题】:Ncurses, non-blocking getch misses first characterNcurses,非阻塞 getch 错过了第一个字符
【发布时间】:2013-07-11 18:13:48
【问题描述】:

我正在为我的应用程序使用 Linux 和 ncurses,并且我正在使用 getch 作为使用 nodelay 的非阻塞。问题是,在使用 getch 循环输入时,它总是会错过第一个字符。例如,输入“Helloworld”将打印为“elloworld”。 目前我似乎没有发现任何问题,但可能是因为我已经盯着代码很久了,或者我错过了一些东西。

    while(TRUE)
{   
    gchar chr;
    gchar *cmd =  g_malloc(50);

    if((getch()) == ERR)
    {
        // no user input
    }
    else
    {
        gint i = 0;

        while((chr = getch()) != '\n')
        {
            cmd[i] = chr;
            waddch(ncurse->window, chr);
            wrefresh(ncurse->window);
            i++;
        }

        waddstr(ncurse->log, cmd);
        wrefresh(ncurse->log);

        wmove(ncurse->window, ncurse->window->_maxy, 2);
        wclrtoeol(ncurse->window);

        wrefresh(ncurse->window);
    }

    g_free(cmd);
}

【问题讨论】:

    标签: c linux nonblocking ncurses getch


    【解决方案1】:

    你期待什么?

    if((getch()) == ERR)
      {
        // no user input
      }
    

    丢弃第一个字符,如果有的话。

    【讨论】:

    • 感谢您的一双新眼睛和正确的推动,现在它已修复。
    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2014-12-16
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多