Reading the Device Status Report ANSI escape sequence reply

前端 未结 4 1146
独厮守ぢ
独厮守ぢ 2020-12-19 02:08

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\");
   s         


        
4条回答
  •  醉梦人生
    2020-12-19 02:35

    #include 
    #include 
    #include 
    
    void readCoord(void* row, void* col){
        int i = 0;
        char* input = malloc(10);
        printf("\e[6n");
        while(!_kbhit()) _sleep(1);
        while(_kbhit()) *(input + (i++)) = getch();
        *(input + i) = '\0';
        sscanf(input, "\e[%d;%dR", row, col);
    }
    
    void main(void){
        int i = 0, r, c;
        char* coord = malloc(10);
        printf("Hello");
        readCoord(&r , &c);
        printf("\nYour coordinate is (%d, %d)", c, r);
    }
    

    _kbhit() is used to detect input (DSR are treated as though typed at the keyboard), and getch() to read and remove the character from standard input

    This program relies on conio.h, which is not a standard and hence not recommended for portable C programs.

提交回复
热议问题