3

I want to be able to move the mouse cursor by means of C# code. Surprisingly there isn't any useful link or previously asked SO question about the same.


What I really want? Just some help to write a console app that runs in a while (true) {...} loop and moves the mouse every five or so minutes.

while (true) {
    MoveMouseToRandomPosition(); //Move mouse to multiple random positions
    Thread.Sleep(5 * 60 * 1000); //Go to sleep for the next five minutes
}

What I really really want? Be able to run this console app when I am working from home on a light day so that my status in outlook does not go Yellow (which means that I have been 'Away' for more than five minutes).


Guys, please do not take this as a 'Give me teh codez' question. Hints are perfectly fine. I can code the hints myself.

displayName
  • 13,888
  • 8
  • 60
  • 75

1 Answers1

1

In WinForms you can do it using the Cursor object.

private void MoveCursor()
{
   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}

Cursor.Position Property

Eins
  • 155
  • 8
  • This one liner also works if you just need to set the cursor to a known coordinate. Also this code does not need to be inside of a Form like the answer code does: `Cursor.Position = new Point(x, y);` – still_dreaming_1 Jun 17 '22 at 12:20