97

I want to simulate mouse movement every x seconds. For that, I'll use a timer (x seconds) and when the timer ticks I'll make the mouse movement.

But, how can I make the mouse cursor move using C#?

Termininja
  • 6,620
  • 12
  • 48
  • 49
aF.
  • 64,980
  • 43
  • 135
  • 198
  • 5
    This sounds like half a solution to a problem you're not telling us about, that probably has more elegant solutions. – Damien_The_Unbeliever Nov 08 '11 at 13:10
  • It's quite possible! We don't understand why but the screen saver is activates passed 10 minutes. But we put 999 minutes :P – aF. Nov 08 '11 at 13:28
  • 3
    Then you ought to be looking for solutions that prevent the screen saver from activating when your application is running, rather than fiddling with the mouse or the screensaver settings. E.g. P/Invoke [SetThreadExecutionState](http://stackoverflow.com/questions/3665332/how-do-i-prevent-screen-savers-and-sleeps-during-my-program-execution/3665545#3665545). I suspected this was screensaver related - programmed mouse movements don't reset the screensaver timer. – Damien_The_Unbeliever Nov 08 '11 at 13:31
  • If he's in the same boat I am, it's because he has a company GP that forces his computer to log him off if it is idle for x num minutes. ;-) – KWallace Nov 20 '21 at 16:03

2 Answers2

101

Take a look at the Cursor.Position Property. It should get you started.

private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   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);
}
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    Thanks @JamesHill, I did not remember how to do this and your example is excellent. I my case I have added some calculations to x and y in order to make mouse movement time related (pixel per second) – Pimenta Oct 31 '12 at 00:33
  • 2
    Is this WinForms approach? – greenoldman Oct 31 '14 at 14:02
  • 20
    I feel like I should mention this so someone doesn't get into the hilarious problem I just had. `Cursor.Clip` will restrict your mouse's movement to the size specified by `Location` and `Size`. So the snippet above will only allow your mouse to move within the application's bounding box. – Brandon Feb 13 '15 at 14:43
  • `Cursor.Position` may require a certain setting if used in a [virtual machine](http://stackoverflow.com/a/22379348/3135228). – Pollitzer Dec 26 '16 at 17:44
  • Works fine, and if the Cursor.Clip line is removed it also works when the window is minimized. – Ben May 04 '17 at 15:56
  • That line `this.Cursor = new Cursor(Cursor.Current.Handle);` gives me an hourglass cursor – barlop Jun 04 '17 at 16:01
  • 1
    For WPF: u have to Add System.Windows.Forms Reference. – Luqqu Dec 18 '20 at 11:44
  • 3
    Lots of people finding this code snippet useful working from home :D – Hanlet Escaño Nov 17 '22 at 15:28
44

First Add a Class called Win32.cs

public class Win32
{ 
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int X, int Y)
        {
            x = X;
            y = Y;
        }
    }
}

You can use it then like this:

Win32.POINT p = new Win32.POINT(xPos, yPos);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);
user3290286
  • 687
  • 1
  • 7
  • 8
  • 1
    where is POINT Type from? – RollRoll Dec 02 '16 at 15:39
  • How do you get the mouse cursor position using this method? – barlop Jun 04 '17 at 17:42
  • this is good.. it should be noted that this is relative to the top left of the form. So it's the same coordinates used by e.g. a control on a form, and the same coordinates used in(and - to answer my q in my comment above - that can be gotten from), the MouseEventArgs e, of eg Form's MouseMove method. – barlop Jun 04 '17 at 23:11
  • 5
    this.Handle implies a WinForm app but you can get a handle to any running window outside of the current running WinForm process. Example: IntPtr desktopWinHandle = Win32.GetDesktopWindow(); where GetDesktopWindow is declared as [DllImport("user32.dll", SetLastError = false)] public static extern IntPtr GetDesktopWindow(); Within the Win32 class. Also checkout the Win32 interop method EnumWindows(). [DllImport("user32.dll")] public static extern int EnumWindows(WndEnumProc lpEnumFunc, int lParam); – Vance McCorkle Feb 12 '21 at 00:14