我正在跟着
中的代码https://gist.github.com/sylt/93d3f7b77e7f3a881603
在这里,如果是404
#include <curses.h>
#include <stdio.h>
int main()
{
initscr();
cbreak();
noecho();
// Enables keypad mode. This makes (at least for me) mouse events getting
// reported as KEY_MOUSE, instead as of random letters.
keypad(stdscr, TRUE);
// Don't mask any mouse events
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
printf("\033[?1003h\n"); // Makes the terminal report mouse movement events
fflush(stdout);
for (;;) {
int c = wgetch(stdscr);
// Exit the program on new line fed
if (c == '\n')
break;
char buffer[512];
size_t max_size = sizeof(buffer);
if (c == ERR) {
snprintf(buffer, max_size, "Nothing happened.");
}
else if (c == KEY_MOUSE) {
MEVENT event;
if (getmouse(&event) == OK) {
snprintf(buffer, max_size, "Mouse at row=%d, column=%d bstate=0x%08lx",
event.y, event.x, event.bstate);
}
else {
snprintf(buffer, max_size, "Got bad mouse event.");
}
}
else {
snprintf(buffer, max_size, "Pressed key %d (%s)", c, keyname(c));
}
move(0, 0);
insertln();
addstr(buffer);
clrtoeol();
move(0, 0);
}
printf("\033[?1003l\n"); // Disable mouse movement events, as l = low
endwin();
return 0;
}
当我将其放入文件并进行编译(gcc -o test test.c -lncurses
)时,在xterm中,无需按任何按钮即可报告鼠标移动。在gnome-terminal和Konsole中,仅在按下鼠标按钮时才报告运动事件!
我通常会说,这只是执行时的兼容性/标准问题:
printf '\033[?1003h'
在gnome-terminal或konsole上,我可以看到报告的所有鼠标移动,而无需按下任何按钮!
那么这是怎么回事?我如何说服gnome-terminal和konsole表现得像xterm?甚至在bash模式下表现得像自己一样?
编辑:其他线索:
如果我滚动鼠标滚轮,此代码似乎可在所有终端上使用。很有可能这就是我最初在xterm中进行测试时发生的情况,因为xterm甚至需要按下按钮或滚动鼠标才能使其正常工作。
因此:我的问题变成了为什么即使在终端以bash模式报告转义序列的情况下,终端仅在对bstate
进行某种修改后才报告鼠标移动?