阅读设备状态报告ANSI转义序列回复 [英] Reading the Device Status Report ANSI escape sequence reply

查看:101
本文介绍了阅读设备状态报告ANSI转义序列回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在VT100终端中检索光标的坐标:

void getCursor(int* x, int* y) {
  printf("\033[6n");
   scanf("\033[%d;%dR", x, y);
}

我正在使用以下ANSI转义序列:

设备状态报告-ESC [6n

将光标位置报告给 ESC [n; mR,其中n为 行,m是列.

代码会编译并发送ANSI序列,但是在接收到该序列后,终端会将^[[x;yR字符串打印到stdout而不是stdin,这使我无法从程序中检索它:

很明显,尽管该字符串是为程序指定的,所以我必须做错了什么.有人知道这是什么吗?

解决方案

您的程序正在运行,但正在等待EOL字符.

scanf是面向行的,因此它在处理之前等待新的一行.尝试运行您的程序,然后按Enter键.

解决方案是使用不需要换行的其他内容来读取输入,然后使用sscanf解析出值.

您还需要使stdin成为非阻塞状态,否则您将无法获得输入,直到缓冲区已满或stdin关闭为止.看到此问题使stdin不受阻碍

您还应该在printf之后调用fflush(stdout);以确保它被实际写入(printf通常是行缓冲的,因此如果没有换行符,它可能不会刷新缓冲区).

I'm trying to retrieve the coordinates of cursor in a VT100 terminal using the following code:

void getCursor(int* x, int* y) {
  printf("\033[6n");
   scanf("\033[%d;%dR", x, y);
}

I'm using the following ANSI escape sequence:

Device Status Report - ESC[6n

Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column.

The code compiles and the ANSI sequence is sent, but, upon receiving it, the terminal prints the ^[[x;yR string to the stdout instead of stdin making it imposible for me to retrieve it from the program:

Clearly, the string is designated for the program, though, so I must be doing something incorrectly. Does anybody know what it is?

解决方案

Your program is working but is waiting for an EOL character.

scanf is line oriented so it waits for a new line before processing. Try running your program and then hit the enter key.

The solution is to use something else that doesn't need a new line to read the input and then use sscanf to parse the values out.

You will also need to make stdin non-blocking or you won't get the input until the buffer is full or stdin is closed. See this question Making stdin non-blocking

You should also call fflush(stdout); after your printf to ensure it is actually written (printf is often line buffered so without a newline it may not flush the buffer).

这篇关于阅读设备状态报告ANSI转义序列回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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