Get cursor position via Windows 10 console VT-100 escape sequence
ansi-escape, batch-file, windows-console
Solution
Major change after three years
It works with reading the response by using `XCOPY` or `REPLACE`.
I'm using `replace` here, to avoid language dependent problems.
@echo off
for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "ESC=%%a"
call :get_cursor_pos
exit /b
:get_cursor_pos
set "response="
set pos=2
:_get_loop
REM *** Request Cursor position
<nul set /p "=%ESC%[6n"
FOR /L %%# in (1 1 %pos%) DO pause < CON > NUL
for /F "tokens=1 skip=1 eol=" %%C in ('"REPLACE /W ? . < con"') DO (
set "char=%%C"
)
set "response=%response%%char%"
set /a pos+=1
if "%char%" NEQ "R" goto :_get_loop
set response
exit /b
The main problem is, `XCOPY` or `REPLACE` allows me to read one character from the input stream, but then clears the remaining buffer.
Conversely, `PAUSE` reads one character, preserving the remaining buffer, but does not reveal what character was read.
To solve this, I issue the query multiple times, reading a different character of the response each time. For each iteration I use a combination of 2 or more `PAUSE` statements followed by `REPLACE` to read a specific character of the response. Each iteration uses one more `PAUSE` than the prior iteration, until I am able to read the terminating R.
I developed this technique and initially posted it at DosTips - Query States using Console Virtual Terminal Sequences.
Problem
I'm playing around with the new (limited) support for VT-100 escape sequences within the Windows 10 console. The supported sequences are documented at https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx. In particular, the following sequence that reports the current cursor position interests me. ``` ESC[6n - responds with ESC[<n>;<m>R, where <n> is the row number, and <m> the column number ``` The response is passed off as keyboard input, and appears on the screen, but I have no idea how to programmatically make use of the information. Ideally I would like to get the `<n>` and `<m>` values into environment variables from within a batch file. But if anyone can demonstrate how to capture the variables using any language, then I may be able to use that knowledge to develop an effective batch file strategy. I can get close with the following simple script called ANSI.BAT ``` @echo off setlocal enableDelayedExpansion for /f "delims=" %%C in ( 'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"' ) do set "esc=%%C" set "csi=%esc%[" echo(Inquiry:%csi%6n set /p "pos=" echo response=!pos:%esc%=ESC! ``` --OUTPUT-- ``` C:\test>ansi Inquiry: ^[[3;9R response=ESC[3;9R C:\test> ``` I can easily parse out the values using FOR /F, once I have the response in a variable. The problem I am having is I must manually press the `<Enter>` key after the response appears on the screen in order to terminate the input for my SET /P statement. I am stumped on where to go from here... EDIT - One last requirement: I don't want the inquiry response to appear on the screen, as that disrupts the screen, and changes the cursor position. I suspect this may be the toughest nut to crack, perhaps impossible with pure batch.