Get Console Cursor Position

Hi all
I need to be able to retrieve the cursor position in a console app. For windows I’m using the GetConsoleScreenBufferInfoEx declare but ideally I need to make this cross-platform. Does anyone know if there is an equivalent api declare in Mac and Linux?

I’ve also tried the ANSI escape code “ESC [ u”, however this prints the result directly to the screen buffer and I can’t see how to intercept this or read it back from the buffer.

Any help appreciated

I’ve tried to port FreePascal’s CRT module to Xojo a few months ago, managed to bring almost all of the features except this one, and console’s width/height sizes.

The ANSI escape code seems to be ESC[6n. Then you’ll have to read the result ESC[<row>;<col>R:

Yes, I’ve pretty much done the same thing myself using the VT escape sequences to enhance the console functionality in Xojo.
I’ve managed to get console dimensions getting/setting working using the API on windows and the stty command on mac/linux.
Going to have another go at trying to do this as there must be a way

If anyone finds this later I’ve managed to produce the working solution below. The cursor position is output to stdin so we simply read until we reach the terminating R character and extract the info. Additionally I disable the echo via another function so the output doesn’t get printed in the terminal window.

Echo = False
stdout.Write ChrB(27)+"[6n"
Echo = True
Dim buf as String
Loop Until buf.Right(1) = "R"
Y = buf.NthField(";",1).MiddleBytes(2).ToInteger
buf = buf.NthField(";",2)
X = buf.LeftBytes(buf.Bytes-1).ToInteger

Note that on Windows I’ve found you also need to disable LineInput via API declares as otherwise stdin will not return unless there is a CRLF in the input stream

1 Like