问题
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.
I'm using batch files to test out the features, and I am able to get almost all of the documented features to work. But I am having trouble with the "Viewport Positioning" sequences (scroll up/down) - In my hands they are totally disfunctional (no effect)
ESC[<n>S - Scroll Up <n> lines
ESC[<n>T - Scroll Down <n> lines
Can anyone get them to work under any circumstances, or is the MS documentation simply wrong?
Also, there are standard inquiry sequences that MicroSoft has not documented, but appear to work.
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.
回答1:
I have NOT Windows 10, so I can't complete any test. However, if the response of the Ansi ESC[6n
sequence is fill the keyboard input buffer with ESC[<n>;<m>R
characters, then it is just necessary to add an Enter key to such input in order to read it via SET /P
command, and this can be done via SendKeys JScript method.
I also used a simpler method to get an ESC character in a variable.
EDIT: I modified the code accordingly to comments...
@if (@CodeSegment == @Batch) @then
@echo off
title Ansi Test
setlocal EnableDelayedExpansion
for /F %%a in ('echo prompt $E ^| cmd') do set "esc=%%a"
set "csi=%esc%["
echo Inquiry:%csi%6n
cscript //nologo //E:JScript "%~F0"
set /p "pos=" > NUL
echo response=!pos:%esc%=ESC!
@end
var sh = WScript.CreateObject("WScript.Shell");
sh.AppActivate("Ansi Test");
sh.SendKeys("{ENTER}");
Please, post the result...
回答2:
I can't test it, I don't want W10.
But I suppose, that it works as SET /P
also works.
It collects the characters with the xcopy /W
trick and stops at the first R
@echo off
setlocal enableExtensions enableDelayedExpansion
call :inquiry
for /L %%n in (1 1 20) do (
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined key set "key=%%L"
)
set "key=!key:~-1!"
set "response=!response!!key!"
if !key!==R goto :break
)
:break
echo !response!
EDIT: Currently it fails
This seems to be a problem of the XCOPY
.
I suppose, that XCOPY
read one character from the input buffer and then it clears the input buffer.
来源:https://stackoverflow.com/questions/38237304/windows-10-console-vt-100-escape-sequences