Search code examples
c#linuxconsolecommand-line-interfacecursor

C# - Get console cursor position on Linux


I'm trying to get the terminal's cursor's position with C#.
I did it on Windows many times. I did it thus:

public Pointer get() {
  Point pos = new Point();
  GetCursorPos(ref pos);
  return pos;
}

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);

Of course this doesn't work on Linux, I'm importing Windows dlls.
How can I do this in linux?


Solution

  • You can use Console.SetCursorPosition

    An example would be like this:

    Console.Clear(); //this clears the console
    Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop); //this sets back to beginning
    

    You can get the current position with this:

    var CurrentCursorPositionConsole = Console.GetCursorPosition();