Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:11:41+00:00 2026-06-07T17:11:41+00:00

When I do this; Point startpoint = Cursor.Position; startpoint.Y -= 1; DoMouse(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE,

  • 0

When I do this;

Point startpoint = Cursor.Position;
startpoint.Y -= 1;
DoMouse(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE, startpoint);

The mouse doesn’t just move up.. it moves a bit to the left as well. But if I do it in a loop, it only moves to the left at the first iteration.

Here is a fully working console program presenting the problem. You have to Add Reference -> .NET -> System.Drawing and System.Windows.Forms to get it to compile.

When starting the program type start to move the mouse up 5 pixels once or type start X (X being a number) to move the mouse up 5 pixels X times. You will see that each new loop the mouse will move a bit to the left; it shouldn’t be doing that at all.

using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mousemove_temp
{
    class Program
    {
        //Capture user input
        static void Main(string[] args)
        {
            while (true)
            {
                string s = Console.ReadLine();

                switch (s)
                {
                    case("start"):
                        moveMouseTest(1);
                        break;
                    default:
                        //Get # of times to run function
                        Match match = Regex.Match(s, @"start (.+)", RegexOptions.IgnoreCase);
                        if (!match.Success || match.Groups.Count != 2) break;

                        //Copy # to int
                        int amnt = -1;
                        try
                        {
                            amnt = Int32.Parse(match.Groups[1].Value);
                        }
                        catch (Exception) { break; } //fail
                        if (amnt <= -1) break; //fail
                        moveMouseTest(amnt); //aaaawww yeah

                        break;
                }

                Thread.Sleep(10);
            }
        }

        //Move the mouse
        static void moveMouseTest(int repeat)
        {
            int countrepeat = 0;

            //Loop entire function X times
            while (countrepeat < repeat)
            {
                Point startpoint = Cursor.Position;
                int amount = 5; //Move 5 pixels
                int counter = 0;

                //Move 1 pixel up each loop
                while (counter < amount)
                {
                    startpoint.Y -= 1;
                    DoMouse(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE, startpoint);

                    counter++;
                    Thread.Sleep(100); //Slow down so you can see it only jumps left the first time
                }

                countrepeat++;
                Console.WriteLine(String.Format("{0}/{1}", countrepeat, repeat));
                Thread.Sleep(1000); //Wait a second before next loop
            }
        }

        /*
         * Function stuff 
        */

        //Control the Mouse
        private static object mouselock = new object(); //For use with multithreading
        public static void DoMouse(MOUSEEVENTF flags, Point newPoint)
        {
            lock (mouselock)
            {
                INPUT input = new INPUT();
                MOUSEINPUT mi = new MOUSEINPUT();
                input.dwType = InputType.Mouse;
                input.mi = mi;
                input.mi.dwExtraInfo = IntPtr.Zero;
                // mouse co-ords: top left is (0,0), bottom right is (65535, 65535)
                // convert screen co-ord to mouse co-ords...
                input.mi.dx = newPoint.X * (65535 / Screen.PrimaryScreen.Bounds.Width);
                input.mi.dy = newPoint.Y * (65535 / Screen.PrimaryScreen.Bounds.Height);
                input.mi.time = 0;
                input.mi.mouseData = 0;
                // can be used for WHEEL event see msdn
                input.mi.dwFlags = flags;
                int cbSize = Marshal.SizeOf(typeof(INPUT));
                int result = SendInput(1, ref input, cbSize);
                if (result == 0)
                    Console.WriteLine("DoMouse Error:" + Marshal.GetLastWin32Error());
            }
        }

        /*
         * Native Methods 
        */

        [DllImport("user32.dll", SetLastError = true)]
        static internal extern Int32 SendInput(Int32 cInputs, ref INPUT pInputs, Int32 cbSize);

        [DllImport("user32.dll")]
        public static extern bool GetAsyncKeyState(Int32 vKey);

        [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 28)]
        internal struct INPUT
        {
            [FieldOffset(0)]
            public InputType dwType;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct MOUSEINPUT
        {
            public Int32 dx;
            public Int32 dy;
            public Int32 mouseData;
            public MOUSEEVENTF dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct KEYBDINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public KEYEVENTF dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct HARDWAREINPUT
        {
            public Int32 uMsg;
            public Int16 wParamL;
            public Int16 wParamH;
        }

        internal enum InputType : int
        {
            Mouse = 0,
            Keyboard = 1,
            Hardware = 2
        }

        [Flags()]
        internal enum MOUSEEVENTF : int
        {
            MOVE = 0x1,
            LEFTDOWN = 0x2,
            LEFTUP = 0x4,
            RIGHTDOWN = 0x8,
            RIGHTUP = 0x10,
            MIDDLEDOWN = 0x20,
            MIDDLEUP = 0x40,
            XDOWN = 0x80,
            XUP = 0x100,
            VIRTUALDESK = 0x400,
            WHEEL = 0x800,
            ABSOLUTE = 0x8000
        }

        [Flags()]
        internal enum KEYEVENTF : int
        {
            EXTENDEDKEY = 1,
            KEYUP = 2,
            UNICODE = 4,
            SCANCODE = 8
        }
    }
}

Can anybody tell what’s going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:11:43+00:00Added an answer on June 7, 2026 at 5:11 pm

    You’re doing the math wrong and as a result are getting rounding errors.

    For example, 65535 / 1920 = 34.1328125. But truncation (because you are dividing an int by an int) is resulting in 34. So if on a 1920×1080 screen you had the mouse all the way at the right, you would get 1920 * (65535 / 1920) = 1920 * 34 = 65280.

    This will get you better results:

                input.mi.dx = (int)((65535.0f * (newPoint.X / (float)Screen.PrimaryScreen.Bounds.Width)) + 0.5f);
                input.mi.dy = (int)((65535.0f * (newPoint.Y / (float)Screen.PrimaryScreen.Bounds.Height)) + 0.5f);
    

    Though if you’re determined to use P/Invoke rather than just say

    Cursor.Position = new Point(newPoint.X, newPoint.Y);
    

    then you really should use SetCursorPos – http://msdn.microsoft.com/en-us/library/windows/desktop/ms648394(v=vs.85).aspx – since that (along with GetCursorPos) is the API that .NET is using to get and set the cursor position via Cursor.Position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a bit confused at this point on what is an object, what
I've edited this quite a bit and bolded my question at this point. I
I wish to determine when a point (mouse position) in on, or near a
At this point I'm not sure if these leaks might be CoreData related or
Up to this point, I have used the following code to restrict the application
Up to this point we have not yet needed a new masterpage for our
I'm convinced at this point that I should be creating subclasses of std::exception for
I have create a module which at this point does nothing but exist the
I'm learning Objective-C but I'm stuck at this point where it comes to multiple
I want to build a 2 dimensional (non ragged at this point) object array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.