簡體   English   中英

如何使用ANSI代碼在C中獲取光標位置

[英]How to get cursor position in C using ANSI code

我試圖從一個小的c程序中獲取光標位置,因此在進行谷歌搜索之后,我發現了此ANSI代碼\\x1b[6n 它應該返回光標的x和y位置(如果我沒記錯的話),所以printf("\\x1b[6n"); 給我輸出: ;1R我無法理解x和y位置的輸出。

編輯:平台是Linux(xterm)

某些終端,諸如DEC VT102和后室速,並在許多終端仿真器,特別的XTerm和它的許多仿制品,發送Esc鍵 [6n將使終端與Esc鍵 [ 響應; R ,其中rowcolumn文本光標位置的小數表示。

因此,您的終端仿真器不會使用;1R ;進行回復。 它正確答復,但readline例程正在吃Esc [和十進制數字 (並根據配置閃爍屏幕或發出鈴聲)。

這是一個不錯的Bash命令來說明:

out=''; \
echo $'\e[6n'; \
while read -n 1 -s -t 1; do out="$out$REPLY"; done < /dev/tty; \
echo -n "$out" | od -A x -t x1z -v

運行此命令可獲得:

$ out=''; \
> echo $'\e[6n'; \
> while read -n 1 -s -t 1; do out="$out$REPLY"; done < /dev/tty; \
> echo -n "$out" | od -A x -t x1z -v

000000 1b 5b 31 36 3b 31 52                             >.[16;1R<
000007

需要注意的是,答案不一定都在標准輸入:答案來自即使標准輸入重定向到的終端

應查詢者的請求,這是一個小型C程序,部分復制了上面腳本的功能。 請注意,該程序無法處理將終端設置為原始模式並返回到熟化模式的問題; 這必須在程序外部進行處理,如下所示。

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main (void)

{
  int ttyfd = open ("/dev/tty", O_RDWR);
  if (ttyfd < 0)
    {
      printf ("Cannot open /devv/tty: errno = %d, %s\r\n",
        errno, strerror (errno));
      exit (EXIT_FAILURE);
    }

  write (ttyfd, "\x1B[6n\n", 5);

  unsigned char answer[16];
  size_t answerlen = 0;
  while (answerlen < sizeof (answer) - 1 &&
         read (ttyfd, answer + answerlen, 1) == 1)
    if (answer [answerlen ++] == 'R') break;
  answer [answerlen] = '\0';

  printf ("Answerback = \"");
  for (size_t i = 0; i < answerlen; ++ i)
    if (answer [i] < ' ' || '~' < answer [i])
      printf ("\\x%02X", (unsigned char) answer [i]);
    else
      printf ("%c", answer [i]);
  printf ("\"\r\n");

  return EXIT_SUCCESS;
}

假設這個小程序是answerback.c

$ gcc -Wall -Wextra answerback.c -o answerback
$ stty raw -echo; ./answerback; stty sane

Answerback = "\x1B[24;1R"
$ _
#include <stdio.h>
#include <termios.h>

int
main() {
 int x = 0, y = 0;
 get_pos(&y, &x);
 printf("x:%d, y:%d\n", x, y);
 return 0;
}

int
get_pos(int *y, int *x) {

 char buf[30]={0};
 int ret, i, pow;
 char ch;

*y = 0; *x = 0;

 struct termios term, restore;

 tcgetattr(0, &term);
 tcgetattr(0, &restore);
 term.c_lflag &= ~(ICANON|ECHO);
 tcsetattr(0, TCSANOW, &term);

 write(1, "\033[6n", 4);

 for( i = 0, ch = 0; ch != 'R'; i++ )
 {
    ret = read(0, &ch, 1);
    if ( !ret ) {
       tcsetattr(0, TCSANOW, &restore);
       fprintf(stderr, "getpos: error reading response!\n");
       return 1;
    }
    buf[i] = ch;
    printf("buf[%d]: \t%c \t%d\n", i, ch, ch);
 }

 if (i < 2) {
    tcsetattr(0, TCSANOW, &restore);
    printf("i < 2\n");
    return(1);
 }

 for( i -= 2, pow = 1; buf[i] != ';'; i--, pow *= 10)
     *x = *x + ( buf[i] - '0' ) * pow;

 for( i-- , pow = 1; buf[i] != '['; i--, pow *= 10)
     *y = *y + ( buf[i] - '0' ) * pow;

 tcsetattr(0, TCSANOW, &restore);
 return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM