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

  • Home
  • SEARCH
  • 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 3284906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:12:25+00:00 2026-05-17T20:12:25+00:00

I have all the necessary code to move and click the mouse via C#

  • 0

I have all the necessary code to move and click the mouse via C# code, but I don’t want to just set the mouse position to X and Y; that will look jerky. Instead, I want to have a smooth transition from point X1, Y1 to point X2, Y2 over Z seconds. Similar to keyframing.

I’m looking for a method similar to this:

public void TransitionMouse(int x, int y, double durationInSecs)

It will just smoothly move the mouse from its current position to x and y in durationInSecs seconds. I have a function called:

public void MoveMouse(int x, int y)

That moves the mouse to x, y immediately.


EDIT

Thanks for the help guys! Here’s the finished, and tested, code:

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

    public void TransitionMouseTo(double x, double y, double durationSecs)
    {
        double frames = durationSecs*100;

        PointF vector = new PointF();
        PointF mousePos = Cursor.Position;

        vector.X = (float)((x - mousePos.X) / frames);
        vector.Y = (float)((y - mousePos.Y) / frames);

        for (int i = 0; i < frames; i++)
        {
            SetCursorPos((int)(mousePos.X += vector.X), (int)(mousePos.Y += vector.Y));
            Thread.Sleep((int)((durationSecs / frames) * 1000.0));
        }
    }
  • 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-05-17T20:12:26+00:00Added an answer on May 17, 2026 at 8:12 pm

    You could do this in several ways. One option would be to calculate the vector required to move the mouse every frame and apply it over the time period to the mouse position.

    So, if we are at position 5,5 and want to move to 20,30 over 10 frames, our vector would be the following:

    val = (target – start) / frames;

    x = (20 – 5) / 10;
    y = (30 – 5) / 10;

    Vector = 1.5,2.5

    Then, in your TransitionMouse method, you apply the vector to the mouse position slowly over whatever duration you wish, using the Thread.Sleep method to control the speed. The code might look like this:

    public void TransitionMouseTo(int x, int y, int durationSecs)
    {
        int frames = 10;
    
        PointF vector = new PointF();
    
        vector.X = (x - Cursor.Position.X) / frames;
        vector.Y = (y - Cursor.Position.Y) / frames;  
    
        for (int i = 0; i < frames; i++)
        {
            Point pos = Cursor.Position;
    
            pos.X += vector.X;
            pos.Y += vector.Y;
    
            Cursor.Position = pos;
    
            Thread.Sleep((durationSecs / frames) * 1000);
        }
    }
    

    Another way of doing it would be to use Bresenhams algorithm to calculate all the points the mouse cursor will move through and then loop through each point and apply it to the cursor again using Thread.Sleep to keep the timing correct.

    Steve

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

Sidebar

Related Questions

I just recently implemented: https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets I have imported all the necessary files into my
I'm trying to run MonoDevelop on apple. I have instaled all the necessary SDK
I have designed one sign-up form,in this form after getting all necessary values I
In case I have .NET framework installed in my computer + all the necessary
I have all the time zone information that is needed and it saves all
I have a site that utilizes a bottom fixed position masthead here: http://www.entheospartners.com/newsite/ This
I have an existing project that I am adding the CoreData framework to but
I am making the paypal application with rails. I have create all the necessary
I have checked out the OpenCV SVN repo, installed all necessary libraries, compiled it
I am busy recreating a powerpoint with xml. i have added all the neccessary

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.