如何读取不完整的表单字段ncurses C ++ [英] How to read an incomplete form field ncurses C++

查看:72
本文介绍了如何读取不完整的表单字段ncurses C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ncurses(C ++)读取表单字段的代码,但是当表单字段未完全键入时,我无法显示值.

I have a code that read a form field using ncurses (C++), but i can't show a value when the form field isn't full typed.

#include <form.h>
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

WINDOW *chatwin, *entrywin;
FIELD  *field[1];
FORM   *fo;

void quit(void)
{
    int i;
    unpost_form(fo);
    free_form(fo);

    free_field(field[0]);
    free_field(field[1]);

    delwin(chatwin);
    endwin();
}

int main(void)
{
    int xsize, ysize;
    int charinput, i;
    char inputstring[200];
    char ttime[10];
    initscr();
    atexit(quit);
    clear();
    noecho();
    curs_set(1);
    cbreak();
    keypad(stdscr, TRUE);

    getmaxyx(stdscr, ysize, xsize);

    start_color();
    use_default_colors();
    init_pair(1, COLOR_YELLOW, COLOR_BLUE);
    init_pair(2, COLOR_BLUE, COLOR_WHITE);

    chatwin = newwin((ysize - 8), (xsize-21), 6, 21);

    entrywin = newwin(1, (xsize-21), (ysize - 1), 21);

    field[0] = new_field(1, (xsize - 21), 0, 0, 0, 10);
    field[1] = 0;
    set_form_win(fo, entrywin);
    fo = new_form(field);
    post_form(fo); 
    field_opts_on(field[0], O_STATIC);
    set_field_fore(field[0], COLOR_PAIR(2));
    set_field_back(field[0], COLOR_PAIR(2));

    refresh();
    wrefresh(chatwin);
    wrefresh(entrywin);

    while((charinput=getch()) != KEY_END)
    {
        switch(charinput)
        {
            case 10:
                snprintf(inputstring, 200, "%s", field_buffer(field[0], 0));
                struct tm *akttime;
                time_t second;
                time(&second);
                akttime = localtime(&second);
                strftime(ttime, 10, "%H:%M:%S", akttime);
                wprintw(chatwin, "<%s> %s|\n", ttime, inputstring);
                wrefresh(chatwin);
                set_field_buffer(field[0], 0, "");
                wrefresh(entrywin);
                break;

            default:
                form_driver(fo, charinput);
                wrefresh(entrywin);
        }
    } 

    return(0);
}

输出

不好.:第二行键入: ejheeh ,但输出为空

Obs.: The second line was typed: ejheeh, but the output was empty

<02:31:42> dddddddkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
                     kkkkkkkkkkk|

<02:31:45>
                                |

<02:31:51> ddddddddddddddddddddddddddddddddddddddddddddddddddddfffffff
                     fffffffffff|

推荐答案

通常,ncurses表单库在离开字段之前不会同步字段缓冲区.由于您正在尝试在离开字段之前读取缓冲区,因此缓冲区没有当前内容.

Typically the ncurses form library does not synchronize the field buffer until you leave the field. Since you are attempting to read the buffer before leaving the field the buffer does not have the current content.

强制缓冲区同步而不实际移动到下一个字段的简单方法是强制字段验证-这将进行同步,然后运行任何验证功能.

A simple way to force the buffer to synchronize without actually moving to the next field is to force field validation -- this will synchronize and then run any validation functions.

case 10:

form_driver(fo, REQ_VALIDATION);

您可能要检查验证错误:

You might want to check for validation errors:

if (form_driver(fo, REQ_VALIDATION) != E_OK) {
    // do something
}

这篇关于如何读取不完整的表单字段ncurses C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆