Page 1 of 1

How to place the cursor

Posted: 11 Dec 2010 16:03
by jeb
Hi,

OK, I know the standard answer: It is not possible with pure batch.

But some minor things are possible and well known.
Like echoing into the same line with the set /p trick

Code: Select all

<nul set /p ".=This echos without CR-LF at the end
<nul set /p ".=...This text is appended in the same line


Then there is the possiblity to delete the text with the <DEL> character,
or to jump with the <CR> to the beginning of the line.
And you can create them with pure batch, without editor-tricks.

<DEL>

Code: Select all

<nul set /p ".=The progress is 50%%"
<nul set /p ".=#!DEL!!DEL!!DEL!!DEL!60%%"


or

<CR>

Code: Select all

<nul set /p ".=The progress is 50%%"
<nul set /p ".=#!CR!The progress is 60%%"


But all these solutions have one problem, you can not move up the cursor even a single line.

Now I found a way to solve this, I tested it with XP and Vista

Code: Select all

@echo off
cls
prompt Main$G
< nul set /p =Press ESC if you see the text "sec"
start /b cmd /k prompt sec$G
ping -n 4 localhost > nul
echo line1
echo line2
echo line3
echo line4
echo line5
echo Press ESC again
exit


How it works:
I start a second cmd.exe-task in the same window (start /b), you can exit one of them, the other will stay open.
In my batch the second cmd.exe-task shows directly the prompt.
I suppose, that the ESC triggers the doskey functionality of the second cmd-task, and it place the cursor and mark the position internally.
To retrieve this internally mark you have to press the ESC-Key again.

And that's the obvious problem of the solution.
The user has to press the ESC-Key, I want to controll it by the batch file itself.

I hope one of the experts (like aGerman, amel, k!, avery_larry, DosItHelp etc .... ) have a good idea.
jeb

Re: How to place the cursor

Posted: 11 Dec 2010 18:38
by aGerman
Well jeb, I believe you cannot emulate the escape key stroke using native batch. ECHOing the ESC character wouldn't succeed.
Probably not what you're looking for because I know you would prefer a solution with pure batch, but it works while the focus remains on the batch window:

Code: Select all

@echo off
>"%temp%\esc.vbs" echo WScript.CreateObject("WScript.Shell").SendKeys "{ESC}"
cls
prompt Main$G
start /b cmd /k prompt sec$G
call "%temp%\esc.vbs"
echo line1
echo line2
echo line3
echo line4
echo line5
call "%temp%\esc.vbs"
del "%temp%\esc.vbs"
exit


Regards
aGerman