OSX iTerm2可以使用python-ncurses鼠标,但不能使用C语言中的curses

时间:2015-04-08 17:08:41

标签: python c ncurses

我正在考虑将鼠标支持包含在用C编写的小编辑器中。编辑器是围绕ncurses构建的,所以我认为它应该是可行的。我从另一篇文章中获得了这个Python源代码并运行:

import curses
from contextlib import contextmanager

@contextmanager
def open_screen():
    scr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    scr.keypad(1)
    try:
        yield scr
    finally:
        curses.nocbreak()
        scr.keypad(0)
        curses.echo()
        curses.endwin()

curses_mouse_states = {
    curses.BUTTON1_PRESSED: 'Button 1 Pressed', 
    curses.BUTTON1_RELEASED: 'Button 1 Released', 
    curses.BUTTON1_CLICKED: 'Button 1 Clicked',
    curses.BUTTON1_DOUBLE_CLICKED: 'Button 1 Double-Clicked',
    curses.BUTTON1_TRIPLE_CLICKED: 'Button 1 Triple-Clicked',

    curses.BUTTON2_PRESSED: 'Button 2 Pressed', 
    curses.BUTTON2_RELEASED: 'Button 2 Released', 
    curses.BUTTON2_CLICKED: 'Button 2 Clicked',
    curses.BUTTON2_DOUBLE_CLICKED: 'Button 2 Double-Clicked',
    curses.BUTTON2_TRIPLE_CLICKED: 'Button 2 Triple-Clicked',

    curses.BUTTON3_PRESSED: 'Button 3 Pressed', 
    curses.BUTTON3_RELEASED: 'Button 3 Released', 
    curses.BUTTON3_CLICKED: 'Button 3 Clicked',
    curses.BUTTON3_DOUBLE_CLICKED: 'Button 3 Double-Clicked',
    curses.BUTTON3_TRIPLE_CLICKED: 'Button 3 Triple-Clicked',

    curses.BUTTON4_PRESSED: 'Button 4 Pressed', 
    curses.BUTTON4_RELEASED: 'Button 4 Released', 
    curses.BUTTON4_CLICKED: 'Button 4 Clicked',
    curses.BUTTON4_DOUBLE_CLICKED: 'Button 4 Double-Clicked',
    curses.BUTTON4_TRIPLE_CLICKED: 'Button 4 Triple-Clicked',

    curses.BUTTON_SHIFT: 'Button Shift', 
    curses.BUTTON_CTRL: 'Button Ctrl', 
    curses.BUTTON_ALT: 'Button Alt'
}

with open_screen() as scr:
    curses.mousemask(-1)
    while True:
        c = scr.getch()
        if c == curses.KEY_MOUSE:
            gmouse = curses.getmouse()
            mouse_state = gmouse[4]
            states = '; '.join(state_string for state, state_string 
                               in curses_mouse_states.viewitems() 
                               if mouse_state & state)
            scr.addstr(0, 0, states)
            scr.clrtoeol()
            scr.addstr(1, 0, "x = %3d" % gmouse[1] )
            scr.addstr(2, 0, "y = %3d" % gmouse[2] )
            scr.refresh()
        elif c == ord('q'):
            break

但是,我在C:

中有这个源代码
#include <ncurses.h>
#include <string.h>

#define WIDTH 30
#define HEIGHT 10 

int startx = 0;
int starty = 0;

char *choices[] = {     "Choice 1",
            "Choice 2",
            "Choice 3",
            "Choice 4",
            "Exit",
        };

int n_choices = sizeof(choices) / sizeof(char *);

void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);

int main()
{   int c, choice = 0;
    WINDOW *myscreen, *menu_win;
    MEVENT  event;
    mmask_t oldmask;

    /* Initialize curses */
    myscreen = initscr();
    clear();
    noecho();
    cbreak();   //Line buffering disabled. pass on everything
    keypad(myscreen,TRUE);

    /* Try to put the window in the middle of screen */
    startx = (80 - WIDTH) / 2;
    starty = (24 - HEIGHT) / 2;

    attron(A_REVERSE);
    mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
    refresh();
    attroff(A_REVERSE);

    /* Print the menu for the first time */
    menu_win = newwin(HEIGHT, WIDTH, starty, startx);
    print_menu(menu_win, 1);
    /* Get all the mouse events */
    mousemask(ALL_MOUSE_EVENTS, &oldmask);

    while(1)
    {
        c = wgetch(menu_win);
        switch(c) {
        case KEY_MOUSE:
            if(getmouse(&event) == OK)
            {   /* When the user clicks left mouse button */
                mvprintw(21,1,"Event: x = %3d y = %3d",event.x,event.y);

                if(event.bstate & BUTTON1_PRESSED)
                {   report_choice(event.x + 1, event.y + 1, &choice);
                    if(choice == -1) //Exit chosen
                        goto end;
                    mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
                    refresh(); 
                }
            } else {
                mvprintw(21,1,"Couldn't get event!");
            }
            print_menu(menu_win, choice);
            break;
        case 'Q':
        case 'q': 
            goto end;
        default:
            break;
        }
    }       
end:
    mousemask(oldmask,&oldmask);
    nocbreak();
    keypad(myscreen,FALSE);
    echo();
    endwin();

    return 0;
}


void print_menu(WINDOW *menu_win, int highlight)
{
    int x, y, i;    

    x = 2;
    y = 2;
    box(menu_win, 0, 0);
    for(i = 0; i < n_choices; ++i)
    {   if(highlight == i + 1)
        {   wattron(menu_win, A_REVERSE); 
            mvwprintw(menu_win, y, x, "%s", choices[i]);
            wattroff(menu_win, A_REVERSE);
        }
        else
            mvwprintw(menu_win, y, x, "%s", choices[i]);
        ++y;
    }
    wrefresh(menu_win);
}

/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{   int i,j, choice;

    i = startx + 2;
    j = starty + 3;

    for(choice = 0; choice < n_choices; ++choice)
        if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
        {   if(choice == n_choices - 1)
                *p_choice = -1;     
            else
                *p_choice = choice + 1; 
            break;
        }
}

这不会运行。显然,初始化的东西是正确的,我开始不理解世界:(任何帮助?

1 个答案:

答案 0 :(得分:2)

C中的示例是从menu_winwgetch(menu_win)上的调用)读取输入,但您没有调用

keypad(menu_win, TRUE);

之前,要启用特殊键。在ncurses中,如果您不启用此模式,则不会返回KEY_MOUSE

wgetch联机帮助页

  

<curses.h>中定义的以下功能键可能   如果键盘已启用,请通过 getch 返回。注意   并非所有这些都必然得到支持   特别的终端。