ncurses表单:显示编辑光标/突出显示活动字段 [英] ncurses forms: Show editing cursor / highlight active field

查看:149
本文介绍了ncurses表单:显示编辑光标/突出显示活动字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,我想在C语言中使用ncurses来实现以下目标: 带有要填写的字段的表格,在此表格的下面,用户在填写表格期间会观察到一个不断变化的传感器值,这将导致所需的操作.

I’m new to programming and I’d like to realise the following with ncurses in C: A form with fields to fill out and underneath this form, there is a continuously changing sensor value to be observed by the user during filling out the form, which results in the desired actions.

我很高兴做到了这一点,现在可以通过按回车键将字段缓冲区放入变量中,但是现在我遇到了一个似乎不可谷歌的问题.

I’m glad I made it that far, that I can put the field buffer into my variables now, by pressing return, but now I’m facing a problem which seems to be not googleable.

我的程序从我在下面发布的示例开始.在原始示例中,我只添加了两行,它已经很好地说明了我的问题.

My program started off from the example, which I posted underneath. In the original example I just added two lines and it already demonstrates my problem very well.

我设置了timeout(1);因此getch()函数在打印新的传感器值之前不会等待用户输入表单. 进入while循环时,我使用mvprint输入了传感器值.

I set timeout(1); so the getch() function won’t wait for user input in the form before it prints the fresh sensor values. Into the while-loop I put in the sennsor value with mvprint.

现在,传感器值始终是最新的,并且仍然可以使用箭头键从一个字段移到另一个字段并在字段中键入内容. 但是可见的游标始终停留在传感器值上,这对我来说很有意义,因为它会不断移动到此处进行打印.表单驱动程序似乎记住了最后编辑的位置,因此仍可以编辑字段,但是没有任何光学提示可以键入该位置.文档将这一位置称为编辑光标".

Now the sensor values are always up-to-date and it is still possible to move from one field to the other with the arrow keys and type into the fields. But the visible Cursor always stays at the sensor value, which makes sense to me, because it is continuously moved there for printing. The forms driver seems to remember the position which was edited lastly, so that editing the fields will still function, but without any optical hint at which position the typing will be. The documentation refers to this position as the "editing-cursor" at one point.

我做错了什么吗?还是有一种方法可以突出显示该字段,甚至可以使编辑光标可见? 谢谢!

Am I doing something completely wrong? Or is there a way to highlight the field, or even make the editing-cursor visible? Thank you!

    /* 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;
}

推荐答案

getch调用实质上是告诉ncurses将光标留在您的mvprintw离开它的地方—在标准屏幕上.要使其移至表单,您必须告诉它使用wgetch,并为当前表单传递WINDOW*指针(该指针又保留该字段在窗口中的位置).

The getch call essentially tells ncurses to leave the cursor where your mvprintw has left it — on the standard screen. To get it to move to your form, you would have to tell it to use wgetch, passing the WINDOW* pointer for the current form (which in turn holds a window's position for the field).

进一步阅读:

  • FIELD *current_field(const FORM *);
  • WINDOW *form_win(const FORM *form);

这篇关于ncurses表单:显示编辑光标/突出显示活动字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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