Page 1 of 1

Get cursor position

Posted: 10 Jun 2021 13:20
by Lordoa
Hello people,

I have this script to get the cursor position, but it is quite slow. Is there a way to make it faster, or even better, native batch?

Code: Select all

@echo off
title Get cursor
cls
setlocal enabledelayedexpansion
if not "%1" == "" goto sendenter

set strlen=for /l %%a in (1,1,2) do if "%%a" == "2" (for /f "tokens=1* delims=," %%b in ("^!args^!") do (if "%%c" == "" (set %%b=0) else ((set tmp$=%%c^& for %%p in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (if not "^!tmp$:~%%p,1^!" == "" (set /a len$+=%%p ^& set tmp$=^^!tmp$:~%%p^^!)))^& set %%b=^^!len$^^!))) else set len$=1^&set args=
FOR /F "delims=#" %%a IN ('"prompt #$E# & for %%a IN (1) do rem"') do set esc=%%a
:menu
echo(
echo(
echo(
pause>nul|set/p =Fourth line, 29th character:
<nul set/p=%esc%[6n
start /b "" "%cd%\%~nx0" cookie
set /p cursorloc=
echo Did it work?
%strlen%result,%cursorloc%
echo %result%
for /f "tokens=1,2,3 delims=[]R;" %%a in ("%cursorloc%") do echo %%b and %%c
pause

:sendenter
powershell -nologo $wshell = New-Object -ComObject wscript.shell; $wshell.Sendkeys('~')
exit
Ty,
Lordoa

Re: Get cursor position

Posted: 10 Jun 2021 13:52
by aGerman
Lordoa wrote:
10 Jun 2021 13:20
... native batch?
No. But why so complicated? You use PowerShell anyway.

Code: Select all

@echo off
title Get cursor
echo(
echo(
echo(
pause>nul|set/p =Fourth line, 29th character:
for /f "tokens=1*" %%i in (
  'powershell.exe -nop -ep Bypass -c "$pos=$host.UI.RawUI.CursorPosition;(''+($pos.X+1)+' '+($pos.Y+1))"'
) do echo line %%j, character %%i
pause
Note that I add 1 to the X and Y position each, just to meet your expectations. Actually that's wrong because coordinates are zero-based. The origin of the console window is {0,0] rather than {1,1}.

Steffen

Re: Get cursor position

Posted: 10 Jun 2021 15:18
by Lordoa
Definitely might use that. :D
You potentially only need to run powershell once

I figured there existed a powershell one-liner to get cursor position, but I was kind of hoping for a native batch way to insert an {ENTER} (to store cursor location in set /p). For example getting the enter key using prompt $_ (but I have no clue how to do that). I'd also be able to use the former in some others uses, like automation.
ANSI syntax has a [1,1] origin,

Thanks,
Lordoa

Re: Get cursor position

Posted: 10 Jun 2021 17:44
by penpen
Thanks to jeb, the cursor position could be read using the vt100 functionality with pure batch:
viewtopic.php?f=3&t=9454&p=61318#p61322

Sidenote: You could do the same using C# to compile an executable file, which is done in the first post...
but then you better might write a c# exe that accesses those information without the need of vt100 sequences.
See https://docs.microsoft.com/en-us/dotnet ... ew=net-5.0.

penpen

Re: Get cursor position

Posted: 10 Jun 2021 21:52
by T3RRY
For pure batch, without powershell, It is much more efficient to use variable tracking of cursor movements than it is to capture and compare the current cursor position - even if you need to perform string length calculations in conjuction with set /a modification to y;x axis variables, you'll still be able to track position much faster than the loop sequence Jeb designed to capture cursor position.

Cursor positioning is indexed from 1;1 - after performing a cls the cursor will be positioned here.
Relative cursor positioning: https://docs.microsoft.com/en-us/window ... ositioning

Out of curiosity, what use case do you think you need the cursor position for?

Re: Get cursor position

Posted: 12 Jun 2021 08:51
by Aacini
You may use my CursorPos.exe auxiliary program to both get and set cursor position. Download it from Advanced Batch features via auxiliary .exe programs.

You will also get a lot of useful auxiliary programs, like GetKey.exe, ColorShow.exe, printf.exe, etc...

Antonio