How to record the mouse drag event and simulate it ?

泽东 钱 20 Reputation points
2024-07-04T15:41:08.25+00:00

I'm a newbie of Rust, and try to write a windows desktop app.

I read the documents then I found SetWindowsHookExA(WH_MOUSE_LL,...)

can record the mouse event, but I only record the mouse down, move and up.

no drag event produced.

I try to simulate them By sending (mouse down, move and up as drag?) to SendInput, but it doesn't work well. (not smoothly)

So I want to know if I use the correct function to record and simulate the mouse event as drag event?

thank you all, best wishes!

My scenario is to record all user input events (keyup, keydown, mouse move, button up, button down, button drag) and then replay them to help the user complete repetitive tasks.

so I need to get the system-level input events, then replay them.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,511 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 43,041 Reputation points
    2024-07-04T21:39:34.2733333+00:00

    I suggest you read the documentation for the DoDragDrop function. There's a lot more to it than just mouse movement. I don't know why you want to simulate dragging, but simulating a drop, which is the end result, makes more sense to me. Along those lines, you may find this article to be of interest -- Reading a contract from the other side: Simulating a drop.


1 additional answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,431 Reputation points Microsoft Vendor
    2024-07-05T08:17:05.8733333+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Record:

    1,You could try to use GetCursorPos to get the the position of the mouse cursor.

    2,You could try to use GetAsyncKeyState to check the state of the physical mouse

    3,And then you could detect the position of the mouse and the status of the keys by looping.

    If the current mouse position or key state is different from the previously saved value, it means that the mouse action has changed, and the current position and key state are recorded.

    4,You could record these information to a script file as a string.

    Replay:

    1,You could read the string in the script file one by one, and the you could call SetCursorPos to move the cursor to the specified screen coordinates.

    2,You could use mouse_event to simulate mouse clicks. It synthesizes mouse motion and button clicks.

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.