我想知道如何在我的程序中获取光标位置 (x, y),而不在屏幕上写任何东西,也不会一直跟踪它。
我找到了一种使用此函数获取其位置的方法(我在这里不检查读取、写入等的返回来编写有关此主题的较小代码,但我在我的程序中执行此操作):
void get_cursor_position(int *col, int *rows)
{
int a = 0;
int i = 0;
char buf[4];
write(1, "\033[6n", 4); // string asking for the cursor position
read(1, buf, 4);
while (buf[i])
{
if (buf[i] >= 48 && buf[i] <= 57)
{
if (a == 0)
*rows = atoi(&buf[i]) - 1;
else
*col = atoi(&buf[i]) - 1;
a++;
}
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数给了我准确的光标位置(*rows = y,*col = x),但它写在屏幕上。
如何在不在屏幕上写任何东西的情况下获得光标位置?
(如果光标位于打印的字符之一上,它将覆盖它。)
应该在发送转义序列之前和之后切换 echo 吗?
这是一个学校项目,所以我只能使用termcap,我不能使用ncurses函数,唯一允许的函数是tputs、tgoto、tgetstr、tgetnum、tgetflag。