【问题标题】:cursor in ncurses window not in the required positionncurses 窗口中的光标不在所需位置
【发布时间】:2017-04-28 17:46:33
【问题描述】:

我正在使用 ncurses 构建自己的终端。我的主窗口包含 5 个其他窗口。我放置在下面的窗口是提示面板。我希望在此处输入文本,并且我希望能够在其中输入文本。我已使用 wmove 定位光标,但光标仍位于窗口的左上角。同样,当我输入文本时,一切都会发生变化。

这是我的代码的一部分;

WINDOW *mainwin, *date_time, *alarm, *colour, *output, *prompt_win;

/* Initialize ncurses */
mainwin = initscr();
if (!mainwin) {
    fprintf(stderr, "Error initialising ncurses.\n");
    exit(EXIT_FAILURE);
}

/* Make our child window, and add a border and some text to it. */
date_time = subwin(mainwin, 5, 35, 0, 0);
box(date_time, 0, 0);

alarm = subwin(mainwin, 5, 35, 0, 35);
box(alarm, 0, 0);
colour = subwin(mainwin, 5, 5, 0, 70);
box(colour, 0, 0);
output = subwin(mainwin, 10, 75, 5, 0);
box(output, 0, 0);
prompt_win = subwin(mainwin, 7, 75, 15, 0);
box(prompt_win, 0, 0);

pid_t pid = fork();

if (pid < 0) {
    perror("Error: Fork Failed");
} else if (pid == 0) {
    /* Alarm & date */
    pid_t child_pid = getpid();
    pid_t pid2 = fork();

    if (pid2 < 0) {
        perror("Error: Fork Failed");
    } else if (pid2 == 0) {
        /* Date */
        pid_t child_pid2 = getpid();

        while (1) {
            wnoutrefresh(date_time);
            getTime(date_time);
            /* doupdate() */
        }
    } else {
        /* Alarm */
        wnoutrefresh(alarm);
        wnoutrefresh(colour);
        /* doupdate() */
    }

    /* _exit(0); */
} else {
    /* Output & prompt */
    /* Display and enter text */

    /* prints "OK>" */
    mvwprintw(prompt_win,curse_loc,1,prompt_arr[0]);
    wnoutrefresh(prompt_win);

    /* gets input */
    getstr(buffer[curse_loc]);
    strcpy(current_cmd,buffer[curse_loc]);
    refresh();

    while (strcmp(current_cmd,"exit")) {
        wmove(prompt_win, curse_loc, sizeof(prompt_arr));
        refresh();
        mvwin(prompt_win, curse_loc, curse_loc);
        wnoutrefresh(prompt_win);
        curse_loc++;
        /* again displays "OK>" */
        mvwprintw(prompt_win, curse_loc, 1, prompt_arr[0]);
        /* gets input */
        getstr(buffer[curse_loc]);
        strcpy(current_cmd, buffer[curse_loc]);
        doupdate();
    }
    endwin();
    delwin(prompt_win);
    delwin(output);
    delwin(colour);
    delwin(alarm);
    delwin(date_time);
    delwin(mainwin);
    refresh();
}

【问题讨论】:

    标签: c ncurses


    【解决方案1】:

    有几个问题:

    • 读取字符的唯一调用(从键盘)是getstr,它(使用stdscr作为它的窗口)停止,光标在当前(例如,wmove ) 在stdscr 中的位置。如果您希望光标停在另一个窗口中,您应该使用wgetstr,将该窗口指针作为参数传递。
    • 程序正试图从两个不同的进程写入屏幕。由于这两个进程不知道对方做了什么更改(也不知道对方发送到屏幕上的内容),因此结果将是不可预测的。

    【讨论】:

    • 如果我希望用户继续在提示中输入内容,而时间无限更新。目前系统不允许用户输入,因为光标已经移动到顶部。我该如何解决这个错误。我曾尝试使用 mvwprintw,因此我认为光标混淆了。
    猜你喜欢
    • 1970-01-01
    • 2016-08-16
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多