简体   繁体   English

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

[英]ncurses not capturing mouse movements but terminal is

I am able to echo -e "\\e[?1003h" and watch as my terminal gobbles up mouse movement events like candies but curses doesn't seem to want them at all. 我能够echo -e "\\e[?1003h"并在终端机echo -e "\\e[?1003h"糖果之类的鼠标移动事件时观看,但是诅咒似乎根本不需要它们。 I looked at 我看着

Mouse movement events in NCurses NCurses中的鼠标移动事件

but it would appear that this issue was resolved by changing the TERM env, which would not help me because my terminal is indeed responding to mouse move events, however, ncurses is not. 但是似乎可以通过更改TERM env解决此问题,但这对我无济于事,因为我的终端确实响应了鼠标移动事件,但是ncurses却没有。 Here's something I have tried (This code came almost entirely from the other question): 这是我尝试过的(此代码几乎完全来自另一个问题):

#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("\033[?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();

}

Additional information and warning: 附加信息和警告:

This program actually accomplishes being able to detect mouse movement AFTER execution. 该程序实际上完成了能够在执行后检测鼠标移动的功能。 This can be reversed with the command echo -e "\\e[?1000h" 可以使用命令echo -e "\\e[?1000h"将其反转

While printf and curses write to the standard output , ncurses will not flush stdout since it does its own buffering. printf和curses写入标准输出时 ,ncurses不会刷新stdout因为它会执行自己的缓冲。 As mentioned in ncurses 6.0 release notes ( August 2015 ): 如ncurses 6.0发行说明( 2015年8月 )所述:

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 . 2012年有关在ncurses中使用信号处理程序的错误报告指出了使用不安全的函数来处理SIGTSTP Other signals could be addressed with workarounds; 其他信号可以通过解决方法解决; repairing SIGTSTP required a different approach. 修复SIGTSTP需要采用不同的方法。 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 . 现在,ncurses可以独立于标准输出缓冲其自身的输出 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. 识别这些应用程序以及完善更改以使低级应用程序能够始终如一地工作需要花费时间。

While the example may happen to work if the printf call is followed by a fflush(stdout) , there is no guarantee that will work indefinitely, since there is no need for ncurses to send its mouse-initialization until the getch call. 尽管在printf调用后接fflush(stdout)可能使示例正常工作,但不能保证它会无限期地工作,因为在getch调用之前不需要ncurses发送其鼠标初始化。 The recommended way to work with ncurses is to put that information into a terminal description, letting ncurses decide when to make changes to the screen. 建议使用ncurses的方法是将信息放入终端描述中,让ncurses决定何时更改屏幕。

There is an example in the ncurses terminal database already: xterm-1003 ncurses终端数据库中已经有一个示例: xterm-1003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM