无法获取箭头键在curses中移动光标(C)

时间:2018-04-28 23:38:34

标签: c ncurses curses keypad

以下是我的代码。我有一个主窗口,有两个选项,我希望用户只需使用KEY_LEFT和KEY_RIGHT就能来回切换。我现在以简单的方式设置它(或者我认为)只需将光标直接移动到其中一个选项即可。

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

int main()
{
    int j = 0, highlight = 0, choice;
    int x = 0, y = 0;

    initscr();
    noecho();
    cbreak();

    WINDOW * mainwin = newwin(20, 52, 0, 0);
    refresh();

    while (1)
    {
        keypad(mainwin, TRUE);
        box(mainwin, 0, 0);
        mvwprintw(mainwin, 1, 1, "             L I C E N S E    P L A T E");
        mvwprintw(mainwin, 3, 2, "RAW ADDITION                          COMPARISON");
        wmove(mainwin, 3, 2);
        wattron(mainwin, A_BLINK);
        refresh();
        wrefresh(mainwin);

        choice = wgetch(mainwin);

       switch(choice)
       {
            case KEY_LEFT:
                wmove(mainwin, 3, 2);
                wrefresh(mainwin);

            case KEY_RIGHT:
                wmove(mainwin, 3, 26);
                wrefresh(mainwin);

            default:
                wmove(mainwin, 3, 2);
                wattroff(mainwin, A_BLINK);
                wrefresh(mainwin);

        }

        if (choice == '\n')
        {
            if (highlight == 0) // RAW ADDITION
            {
                char lp[8];
                char num[10];
                int n;
                WINDOW * win = subwin(mainwin, 30, 45, 1, 1);
                refresh();

                box(win, 1, 1);
                mvwprintw(win, 2, 2, "RAW ADDITION");
                mvwprintw(win, 3, 2, "Enter a license plate and an integer.");
                mvwprintw(win, 5, 3, "._________.          ._____________.");
                mvwprintw(win, 6, 3, "|         |          |             |");
                mvwprintw(win, 7, 3, "'---------'          '-------------'");

                wmove(win, 6, 5);
                wrefresh(win);

                mvwgetnstr(win, 6, 5, lp, 7);
                wrefresh(win);

                wmove(win, 6, 26);
                wrefresh(win);

                mvwgetnstr(win, 6, 26, num, 10);
                wrefresh(win);

                n = atoi(num);
            }
            else // COMPARISON
            {

            }
        }

        if (toupper(choice) == 'Q')
            break;
        else if (choice == KEY_LEFT)
        {
            wmove(mainwin, 3, 2);
            wrefresh(mainwin);
        }
        else if (choice == KEY_RIGHT)
        {
            wmove(mainwin, 3, 26);
            wrefresh(mainwin);
        }
    }

    endwin();
    exit(EXIT_SUCCESS);
    return 0;
}

在上面的代码中,我需要在用户按下左箭头键或右箭头键时尝试捕获的代码块,并将窗口中的光标移动到适当的选择(RAW ADDRESS或COMPARISON)。但是,出于某种原因,我无法得到任何回应箭头键的内容。 wmove()函数或键盘()函数是否缺少某些东西?我在这里不理解什么? move函数是否相对于光标的最后位置移动了?

谢谢!

1 个答案:

答案 0 :(得分:0)

switch语句中的wmove调用没有太大作用,因为导致wgetch的逻辑将光标移回同一点:

    wmove(mainwin, 3, 2);
    wattron(mainwin, A_BLINK);
    refresh();
    wrefresh(mainwin);

    choice = wgetch(mainwin);

顺便说一句,在快速阅读来源时,似乎不需要refresh()次来电...