2017-04-26 46 views
0

我是编程新手,我想用C语言中的ncurses实现以下内容: 带有填充字段的表单,并且在此表单下方有一个连续的改变传感器值以在填写表格期间由用户观察,这导致期望的动作。ncurses形式:显示编辑光标/高亮活动字段

我很高兴我做到了这一点,我现在可以将字段缓冲区放入我的变量中,按回车键,但现在我面临的问题似乎是不可Googleable的。

我的程序从我在下面发布的示例开始。在原始示例中,我只添加了两行,它已经很好地说明了我的问题。我设定超时(1);所以getch()函数在打印新的传感器值之前不会等待表单中的用户输入。 在while循环中,我使用mvprint放入传感器值。

现在传感器值始终是最新的,并且仍然可以使用箭头键从一个字段移动到另一个字段并键入字段。 但是可见的光标始终停留在传感器值,这对我来说很合理,因为它不断移动到打印位置。表单驱动程序似乎记住了最后编辑的位置,因此编辑这些字段仍然可以正常工作,但是没有任何光学提示将键入的位置。文档将此位置称为“编辑光标”。

我在做一些完全错误的事情吗?或者有什么方法可以突出显示该字段,甚至可以使编辑光标可见? 谢谢!

/* gcc -Wall -pthread -g -o formncurses formncurses.c -lform -lncurses */ 

    #include <form.h> 

    int main() 
    { FIELD *field[3]; 
    FORM *my_form; 
    int ch; 

    /* Initialize curses */ 
    initscr(); 
    cbreak(); 
    noecho(); 
    keypad(stdscr, TRUE); 

    timeout(1); 

    /* Initialize the fields */ 
    field[0] = new_field(1, 10, 4, 18, 0, 0); 
    field[1] = new_field(1, 10, 6, 18, 0, 0); 
    field[2] = NULL; 

    /* Set field options */ 
    set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 
    field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 
         /* Field is filled up  */ 
    set_field_back(field[1], A_UNDERLINE); 
    field_opts_off(field[1], O_AUTOSKIP); 

    /* Create the form and post it */ 
    my_form = new_form(field); 
    post_form(my_form); 
    refresh(); 

    mvprintw(4, 10, "Value 1:"); 
    mvprintw(6, 10, "Value 2:"); 
    refresh(); 

    /* Loop through to get user requests */ 
    while((ch = getch()) != KEY_F(1)) 
    { switch(ch) 
     { case KEY_DOWN: 
       /* Go to next field */ 
       form_driver(my_form, REQ_NEXT_FIELD); 
       /* Go to the end of the present buffer */ 
       /* Leaves nicely at the last character */ 
       form_driver(my_form, REQ_END_LINE); 
       break; 
      case KEY_UP: 
       /* Go to previous field */ 
       form_driver(my_form, REQ_PREV_FIELD); 
       form_driver(my_form, REQ_END_LINE); 
       break; 
      default: 
       /* If this is a normal character, it gets */ 
       /* Printed    */  
       form_driver(my_form, ch); 
       break; 
     } 

    mvprintw(12, 10, "Here stands the changing sensor value"); 

    } 

    /* Un post form and free the memory */ 
    unpost_form(my_form); 
    free_form(my_form); 
    free_field(field[0]); 
    free_field(field[1]); 

    endwin(); 
    return 0; 
} 

回答

0

getch通话基本上是让Ncurses把光标停留在您mvprintw已经离开它—的标准屏幕上。为了让它移动到你的表单中,你必须告诉它使用wgetch,将WINDOW*指针传递给当前表单(这反过来又保留了一个窗口的位置)。

延伸阅读:

+0

太好了!现在有了一个额外的窗口wgetch它的工作。其实我以前试过这个,显然是犯了一些错误,所以除非我按下一个键,否则这个值不会更新。所以我(错误地)总结了wgetch不受超时(1)的影响。非常感谢! – Wonnegrausen

相关问题