How do I set the position of the mouse cursor from a Console app in C#?

guywhoneedsahand picture guywhoneedsahand · Jul 16, 2011 · Viewed 11.4k times · Source

I've found many articles on how to set the mouse position in a C# windows forms project - I want to do this in a console application. How can I set the absolute mouse position from a C# windows console application?

Thanks!

Hint: it's not Console.setCursorPosition, that only sets the position of the text cursor in the console.

Answer

Chaz picture Chaz · Mar 16, 2016

This is an old thread, but for the sake of completion it can be done this way...

use System.Runtime.InteropServices;

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

then in method whatever position you wish e.g.

  SetCursorPos(500, 500);