Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 399
guywhoneedsahand Avatar asked Jul 16 '11 08:07

guywhoneedsahand


People also ask

How do I change cursor position?

SetCursorPosition(Int32, Int32) Method is used to set the position of cursor. Basically, it specifies where the next write operation will begin in the console window.

Which function is used to shift the cursor from current position to desired position?

You can also use another function SetCursorPos to set the cursor position. The SetCursorPos function can be used in a looping structure to move the cursor across the screen.

What indicates the position of the mouse on the screen?

1) A cursor is the position indicator on a computer display screen where a user can enter text. In an operating system with a graphical user interface (GUI), the cursor is also a visible and moving pointer that the user controls with a mouse, touch pad, or similar input device.


1 Answers

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);
like image 67
Chaz Avatar answered Sep 18 '22 22:09

Chaz