ncurses 不捕获鼠标移动,但终端是

ncurses not capturing mouse movements but terminal is

我能够 echo -e "\e[?1003h" 并看着我的终端吞噬鼠标移动事件,例如糖果,但 curses 似乎根本不需要它们。我看了

Mouse movement events in NCurses

但似乎通过更改 TERM env 解决了这个问题,这对我没有帮助,因为我的终端确实响应了鼠标移动事件,但是 ncurses 没有。这是我尝试过的东西(这段代码几乎完全来自另一个问题):

#include <ncurses.h>
#include <assert.h>

int main(){
        int ch, count=0;
        mmask_t old;

        initscr ();
        noecho ();
        cbreak ();
        mousemask (ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &old);
        keypad (stdscr, TRUE);
        printf("3[?1003h");

        while ((ch = getch ()) != 'q')
        {
          count++;
          if (ch == KEY_MOUSE)
          {
             MEVENT event;
             assert (getmouse (&event) == OK);
             mvprintw (0, 0, "Mouse Event!\n");
          }
          mvprintw (1, 1, "Event number %4d",count);
          refresh();
        }
        endwin();

}

附加信息和警告:

这个程序实际上实现了能够在执行后检测鼠标移动。这可以通过命令 echo -e "\e[?1000h"

来反转

printf 和 curses 写入 标准输出 时,ncurses 不会刷新 stdout 因为它做自己的缓冲。如 ncurses 6.0 发行说明中所述 (August 2015):

Output buffering provided a further, but worthwhile distraction. A bug report in 2012 regarding the use of signal handlers in ncurses) pointed out a problem with the use of unsafe functions for handling SIGTSTP. Other signals could be addressed with workarounds; repairing SIGTSTP required a different approach. The solution required changing internal behavior of the library: how it handles output buffering.

Now ncurses buffers its own output, independently of the standard output. A few applications relied upon the library's direct reuse of the standard output buffering; however that is unspecified behavior and has never been a recommended practice. Identifying these applications as well as refining the change to permit low-level applications to work consistently took time.

虽然如果 printf 调用后跟 fflush(stdout),示例可能会起作用,不能保证会无限期地工作,因为在 getch 调用之前 ncurses 不需要发送其鼠标初始化。使用 ncurses 的推荐方法是将该信息放入终端描述中,让 ncurses 决定何时对屏幕进行更改。

ncurses 终端数据库中已有示例:xterm-1003