CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2017
    Posts
    5

    Post Simulate cursor ( mouse ) movement

    Code:
    int x = 100;
    int y = 100;
           
    SetCursorPos(x,y);
    Sleep(2000);
    SetCursorPos(x+100,y+200);
    This code sets cursot to one dot, after 2 seconds to another dot.
    However cursor just "teleporting".
    And I want to make cursor to "walk" from one dot to another, not just "teleport".

    Its kinda like demonstration. Cursor should stay visible ( and it is ), i simply want to make 'travelling route'.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simulate cursor ( mouse ) movement

    Use a loop and move the cursor in small increments until it reaches its destination.

  3. #3
    Join Date
    Jul 2017
    Posts
    5

    Re: Simulate cursor ( mouse ) movement

    I expected advanced variant, but ok. I made as you suggest already.
    I have extra question, it is related.

    Code:
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    What 3rd and 4th parameter require? Is it for X-Y-Z asis ??

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simulate cursor ( mouse ) movement

    Quote Originally Posted by programm View Post
    I expected advanced variant, but ok. I made as you suggest already.
    I have extra question, it is related.

    Code:
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    What 3rd and 4th parameter require? Is it for X-Y-Z asis ??
    You may have expected code, but putting the code you have in a loop is trivial, and if you are asking for such code, it might be a better exercise for you to do yourself.

    For the mouse_event api question there is the official Microsoft msdn documentation: https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Note that the mouse_event api is deprecated and that the SendInput api should be used instead. SendInput msdn documentation can be found at https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Code snippets for both API's can be found in bing or google by searching for "SendInput examples" (or "mouse_event examples").

  5. #5
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Re: Simulate cursor ( mouse ) movement

    You could also:

    1- stablish a timer event at 1/15s (for example)
    2- at every time this event is hit you call SetCursorPos with the new values

    This way you won´t block the aplication in the loop.
    If the movement is a line you can calculate dx and dy values before set the timer and increment the mouse positions at avery time the timer is hit with these values.
    If the movement is not a line, a parabola for example, you will have to calculate every position when the timer is hit. Remember that the dt (time lapse) is constant so this will not be dificult to calculate the new position.
    Last edited by Rabelo; July 20th, 2017 at 06:15 PM. Reason: error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured