【问题标题】:Ncurses bug mouse coordinates to beyond 222 width or height terminalNcurses错误鼠标坐标超过222宽度或高度终端
【发布时间】:2013-05-22 14:40:10
【问题描述】:

我正在使用 ncurses 编写用户界面。 当我的终端宽度或高度超过 222 个字符/行时,我遇到问题,鼠标坐标返回 -33 值,所以当我点击超过 222 个字符时 mouse_x = -33 当我点击超过 222 行 mouse_y = -33

是否可以告诉 ncurses 不停止超过 222 个字符/行的鼠标事件? 这个错误也出现在 vim 中,当您将 :vs 分隔条移动到 222 个字符之外时,它会回到 x=0。

也许这个错误已经修复了?

  1 #include <ncurses.h>
  2 
  3 void    treat_mouse(MEVENT* event);
  4 
  5 int     main(void)
  6 {
  7         bool    run;
  8         int     key;
  9         MEVENT  event;
 10 
 11         run = true;
 12         initscr();
 13         noecho();
 14         keypad(stdscr, TRUE);
 15         mousemask(BUTTON1_PRESSED, NULL);
 16         while (run == true)
 17         {
 18                 key = getch();
 19                 switch  (key)
 20                 {
 21                         case    'q':
 22                                 run = false;
 23                                 break;
 24                         case    KEY_MOUSE:
 25                                 treat_mouse(&event);
 26                                 break;
 27                         default:
 28                                 break;
 29                 }
 30         }
 31         endwin();
 32         return 0;
 33 }
 34 
 35 void    treat_mouse(MEVENT* event)
 36 {
 37         if (getmouse(event) == OK)
 38                 mvwprintw(stdscr, 0, 0, "click: x = %d, y = %d\n", event->x, event->y);
 39 } 

.

.

=========================编辑====================== =

.

.

好的,我找到了。

我在这里下载了ncurses代码源http://ftp.gnu.org/pub/gnu/ncurses/

我已使用此链接 ncurses-5.9.tar.gz 04-Apr-2011 19:12 2.7M

我已经搜索过 getch()

getch() 调用 wgetch()

wgetch() 调用 _nc_wgetch()

_nc_wgetch() 调用 _mouse_inline()

_mouse_inline()struct screen 中调用 _nc_mouse_inline()no_mouse_inline() 的函数指针谁是一个空函数(我认为没有鼠标时)。

您可以在 ncurses-5.9/ncurses/base/lib_mouse.c

中看到 _nc_mouse_inline() 函数

她使用 unsigned char 来计算鼠标坐标,看看这个小例子:

821 static bool
822 _nc_mouse_inline(SCREEN *sp)
823 /* mouse report received in the keyboard stream -- parse its info */
824 {
.
.
.
832         unsigned char kbuf[4];
.
.
.
970         eventp->x = (kbuf[1] - ' ') - 1;
971         eventp->y = (kbuf[2] - ' ') - 1;
.
.
.
984     return (result);
985 }

最后我会做任何事情。

【问题讨论】:

    标签: c events mouse ncurses curses


    【解决方案1】:

    我认为您已经找到了问题的答案。

    unsigned char kbuf[4];
    eventp->x = (kbuf[1] - ' ') - 1;
    eventp->y = (kbuf[2] - ' ') - 1;
    

    鼠标 x 和 y 被接收为 unsigned char,所以它们可以容纳的最大值是 255。' '(空格)是 ASCII 32,所以可能的最大值是 255 - 32 - 1,等于 222。

    然后该值换行为 0,给出 0 - 32 -1,等于 -33。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-22
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    相关资源
    最近更新 更多