Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Lordoa
- Posts: 5
- Joined: 26 May 2021 09:07
#1
Post
by Lordoa » 10 Jun 2021 13:20
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
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 10 Jun 2021 13:52
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
-
Lordoa
- Posts: 5
- Joined: 26 May 2021 09:07
#3
Post
by Lordoa » 10 Jun 2021 15:18
Definitely might use that.
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
-
T3RRY
- Posts: 250
- Joined: 06 May 2020 10:14
#5
Post
by T3RRY » 10 Jun 2021 21:52
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?
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#6
Post
by Aacini » 12 Jun 2021 08:51
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