如何在ncurses程序中更改F1键的默认操作?

时间:2014-04-20 10:53:47

标签: bash ncurses keymapping

我正在使用fedora 20并通过学习示例程序来学习ncurses编程。我已经在NCURSES Programming HOWTO中找到了一个(在我的文件“border.c”下面列出,请参阅注释),按下功能键F1时应退出:相反,它给了我一个标题为“终端帮助”的窗口,所以我使用CTRL -C退出。我正在使用以下命令从bash shell命令窗口编译和运行程序:

[Harry@localhost ~]$ gcc -o border.o border.c -l ncurses
[Harry@localhost ~]$ ./border.o

拜托,我可以以某种方式覆盖这个F1动作,以便ncurses程序读取并按下按键操作,并且最好还在其他地方保留其默认动作吗?

注意TAB的效果在草稿中是可以的,但实际问题中显示的有点乱。

#include <ncurses.h>

typedef struct _win_border_struct {
    chtype  ls, rs, ts, bs, 
        tl, tr, bl, br;
}WIN_BORDER;

typedef struct _WIN_struct {

    int startx, starty;
    int height, width;
    WIN_BORDER border;
}WIN;

void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, bool flag);

int main(int argc, char *argv[])
{   WIN win;
    int ch;

    initscr();          /* Start curses mode        */
    start_color();          /* Start the color functionality */
    cbreak();           /* Line buffering disabled, Pass on
                     * everty thing to me       */
    keypad(stdscr, TRUE);       /* I need that nifty F1     */
    noecho();
    init_pair(1, COLOR_CYAN, COLOR_BLACK);

    /* Initialize the window parameters */
    init_win_params(&win);
    print_win_params(&win);

    attron(COLOR_PAIR(1));
    printw("Press F1 to exit");
    refresh();
    attroff(COLOR_PAIR(1));

    create_box(&win, TRUE);
    while((ch = getch()) != KEY_F(1))
    {   switch(ch)
        {   case KEY_LEFT:
                create_box(&win, FALSE);
                --win.startx;
                create_box(&win, TRUE);
                break;
            case KEY_RIGHT:
                create_box(&win, FALSE);
                ++win.startx;
                create_box(&win, TRUE);
                break;
            case KEY_UP:
                create_box(&win, FALSE);
                --win.starty;
                create_box(&win, TRUE);
                break;
            case KEY_DOWN:
                create_box(&win, FALSE);
                ++win.starty;
                create_box(&win, TRUE);
                break;  
        }
    }
    endwin();           /* End curses mode        */
    return 0;
}
void init_win_params(WIN *p_win)
{
    p_win->height = 3;
    p_win->width = 10;
    p_win->starty = (LINES - p_win->height)/2;  
    p_win->startx = (COLS - p_win->width)/2;

    p_win->border.ls = '|';
    p_win->border.rs = '|';
    p_win->border.ts = '-';
    p_win->border.bs = '-';
    p_win->border.tl = '+';
    p_win->border.tr = '+';
    p_win->border.bl = '+';
    p_win->border.br = '+';

}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
    mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, 
                p_win->width, p_win->height);
    refresh();
#endif
}
void create_box(WIN *p_win, bool flag)
{   int i, j;
    int x, y, w, h;

    x = p_win->startx;
    y = p_win->starty;
    w = p_win->width;
    h = p_win->height;

    if(flag == TRUE)
    {   mvaddch(y, x, p_win->border.tl);
        mvaddch(y, x + w, p_win->border.tr);
        mvaddch(y + h, x, p_win->border.bl);
        mvaddch(y + h, x + w, p_win->border.br);
        mvhline(y, x + 1, p_win->border.ts, w - 1);
        mvhline(y + h, x + 1, p_win->border.bs, w - 1);
        mvvline(y + 1, x, p_win->border.ls, h - 1);
        mvvline(y + 1, x + w, p_win->border.rs, h - 1);

    }
    else
        for(j = y; j <= y + h; ++j)
            for(i = x; i <= x + w; ++i)
                mvaddch(j, i, ' ');

    refresh();

}

1 个答案:

答案 0 :(得分:0)

类似的问题确实发布在Stackoverflow中,答案是它在系统的各个方面之间不兼容,这看起来很难克服:所以,我现在将避免使用这些笨拙的F键。

经过实验,我现在发现F(2)到F(9)和F(12)工作正常; F(10)打开工具栏;和F(11)打开和关闭全屏;除了我询问的F(1)的影响。

感谢edvinas指出我应该使用Stackoverflow,以及那些迁移它的人

我会尽快接受这个答案,以便在此处保存更多评论