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 9137525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:06:50+00:00 2026-06-17T09:06:50+00:00

I wanted to write a procedure in delphi to simulate a moving mouse pointer

  • 0

I wanted to write a procedure in delphi to simulate a moving mouse pointer with a specific speed (similar to the AutoIT MouseMove function).
Either my code is wrong or SetCursorPos malfunctions after it gets called too many times.
Here is the function I have:

procedure MoveMouse ( X, Y, Speed : Integer);
var
  P     : TPoint;
  NewX  : Integer;
  NewY  : Integer;
begin
  if X < 0 then exit;
  if Y < 0 then exit;
  if X > Screen.Height then exit;
  if Y > Screen.Width then Exit;
  repeat
    GetCursorPos(P);
    NewX := P.X;
    NewY := P.Y;
    if P.X <> X then begin
      if P.X > X then begin
        NewX := P.X - 1;
      end else begin
        NewX := P.X + 1;
      end;
    end;
    if P.Y <> Y then begin
      if P.Y > Y then begin
        NewY := P.Y - 1;
      end else begin
        NewY := P.Y + 1;
      end;
    end;
    sleep (Speed);
    SetCursorPos(NewX, NewY);
  until (P.X = X) and (P.Y = Y);
end;

I use it like this:

procedure TForm1.btn1Click(Sender: TObject);
var
  X : Integer;
  Y : Integer;
begin
  for X := 0 to Screen.Width do begin
    for Y := 0 to Screen.Height do begin
      MouseClick (X, Y, 1);
    end;
  end;
end;

For some reason the mousepointer gets stuck at a certain X point and then jumps back to 0,0 but why is that?

  • 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-17T09:06:51+00:00Added an answer on June 17, 2026 at 9:06 am

    You code is stuck , because in the repeat loop, the condition

    until (P.X = X) and (P.Y = Y);
    

    is never satisfied when you pass the values X=0 and Y=Screen.Height, so you must modify your loop to pass only valid screen coordinates values

      for X := 0 to Screen.Width-1 do
        for Y := 0 to Screen.Height-1 do
          MoveMouse (X, Y, 1); 
    

    Also you can improve your method checking the result of the GetCursorPos and SetCursorPos functions.

    procedure MoveMouse ( X, Y, Speed : Word);
    var
      P     : TPoint;
      NewX  : Integer;
      NewY  : Integer;
    begin
      if X > Screen.Width-1  then Exit;
      if Y > Screen.Height-1 then Exit;
      repeat
        if not GetCursorPos(P) then RaiseLastOSError;
        NewX := P.X;
        NewY := P.Y;
        if P.X <> X then
        begin
          if P.X > X then
            NewX := P.X - 1
          else
            NewX := P.X + 1;
        end;
    
        if P.Y <> Y then
        begin
          if P.Y > Y then
            NewY := P.Y - 1
          else
            NewY := P.Y + 1
        end;
        Sleep (Speed);
        if not SetCursorPos(NewX, NewY) then RaiseLastOSError;
      until (P.X = X) and (P.Y = Y);
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to write a function that'll be cross platform (win32 & linux), and
I wanted to write a function that would take an object and convert it
I wanted to write a function with a variable argument list. I want to
I wanted to write a function that reverses all the sub-lists in a list
I wanted to write a query to delete from x table and y table
I wanted to write something basic in assembly under Windows. I'm using NASM, but
I wanted to write a regex to count the number of spaces/tabs/newline in a
Just wanted to write some recursion but can't check if the child is in
I wanted to write following query through codeigniter's db helper class, guide me plz
I wanted to write a method with an argument that defaults to a member

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.