繁体   English   中英

Ncurses鼠标的坐标超出了222的宽度或高度

[英]Ncurses bug mouse coordinates to beyond 222 width or height terminal

我正在用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 2011年4月4日19:12 2.7M

我已经搜索了getch()

getch()调用wgetch()

wgetch()调用_nc_wgetch()

_nc_wgetch()调用_mouse_inline()

_mouse_inline()结构屏幕中的函数指针,它们调用_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 }

最后,我会做任何事情。

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

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 0 - 32 -1 ,等于-33。

暂无
暂无

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

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