ncurses不能捕获鼠标移动,但是终端可以 [英] ncurses not capturing mouse movements but terminal is

查看:272
本文介绍了ncurses不能捕获鼠标移动,但是终端可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够echo -e "\e[?1003h"并观察终端机吞噬糖果之类的鼠标移动事件,但诅咒似乎根本不需要它们.我看着

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. I looked at

NCurses中的鼠标移动事件

,但是似乎可以通过更改TERM env解决此问题,但这对我没有帮助,因为我的终端确实响应了鼠标移动事件,但是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. 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();

}

其他信息和警告:

该程序实际上完成了在执行后能够检测鼠标移动的功能.可以使用命令echo -e "\e[?1000h"

This program actually accomplishes being able to detect mouse movement AFTER execution. This can be reversed with the command echo -e "\e[?1000h"

推荐答案

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

While printf and curses write to the standard output, ncurses will not flush stdout since it does its own buffering. As mentioned in ncurses 6.0 release notes (August 2015):

输出缓冲提供了进一步但值得的注意力. 2012年有关在ncurses中使用信号处理程序的错误报告指出了使用不安全的函数来处理SIGTSTP的问题.其他信号可以通过变通办法解决;修复SIGTSTP需要另一种方法.该解决方案需要更改库的内部行为:库如何处理输出缓冲.

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.

现在ncurses可以独立于标准输出来缓冲其自身的输出.一些应用程序依赖于库对标准输出缓冲的直接重用.但是,这是未指定的行为,从来都不是推荐的做法.识别这些应用程序以及优化更改以使低级应用程序能够始终如一地工作需要花费时间.

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决定何时更改屏幕.

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. 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终端数据库中已经有一个示例: xterm-1003

There is an example in the ncurses terminal database already: xterm-1003

这篇关于ncurses不能捕获鼠标移动,但是终端可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆