OSX iTerm2 可以将鼠标与 python-ncurses 一起使用,但不能与 C 中的 curses 一起使用

OSX iTerm2 can use mouse with python-ncurses but not with curses in C

我正在考虑将鼠标支持添加到用 C 编写的小型编辑器中。该编辑器是围绕 ncurses 构建的,所以我认为这应该是可能的。我从另一个 post 得到了这个 Python 来源并且它 运行s:

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

而这不是 运行。显然,初始化的东西是正确的,我开始不了解这个世界:(有什么帮助吗?

C 中的示例正在读取来自 menu_win 的输入(对 wgetch(menu_win) 的调用),但您没有调用

keypad(menu_win, TRUE);

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

wgetch 联机帮助页说

The following function keys, defined in <curses.h>, might be returned by getch if keypad has been enabled. Note that not all of these are necessarily supported on any particular terminal.