1

I want to test a flash application running on browser. I need to drag pieces around in the application. I want to do this by simulating mouse movements using windows api.

Here is my navie solution to simulate a mouse drag:

            void MoveMouse(LPINPUT input, int x, int y) {
                input->type = INPUT_MOUSE;
                input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
                input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
                input->mi.dwFlags =  MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
            }

            void LeftDownMouse(LPINPUT input, int x, int y) {
                input->type = INPUT_MOUSE;
                input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
                input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
                input->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
            }

            void LeftUpMouse(LPINPUT input, int x, int y) {
                input->type = INPUT_MOUSE;
                input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
                input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
                input->mi.dwFlags = MOUSEEVENTF_LEFTUP;
            }

            void DragMouse(LPINPUT inputs, int startX, int startY, int dropX, int dropY) {
                MoveMouse(&inputs[0], startX, startY);
                LeftDownMouse(&inputs[1], startX, startY);
                LeftUpMouse(&inputs[2], startX, startY);
                LeftDownMouse(&inputs[3], startX, startY);
                MoveMouse(&inputs[4], dropX, dropY);
                LeftUpMouse(&inputs[5], dropX, dropY);
            }

            int _tmain(int argc, _TCHAR* argv[])
            {
                INPUT inputs [6] = {};
                                    // Assume there is a maximized window, mouse drags the window from 40, 10 to 100,100 coordinates.
                DragMouse(inputs, 40, 10, 100, 100);

                SendInput(6, inputs, sizeof(INPUT));


                Sleep(1000);

                return 0;
            }

This code mostly doesn't work, sometimes works, it has undefined behaviour. What could be the problem? How can i solve this?

9
  • Why don't you use a supported way to do this rather than faking input? Apr 20, 2014 at 6:13
  • 4
    why don't you suggest something rather than ask me? Apr 20, 2014 at 13:33
  • I'm wondering if there is a specific reason for your choice of solution. Because it seems odd to me. So perhaps there is some motivation that I am not aware of behind your decision to fake input to get this done. Are you wedded to faking input, or are you prepared to consider other solutions? Apr 20, 2014 at 13:38
  • I can use anything that works. The application lives on the web, and i need to test it live. Also i want to know how to fake mouse input in anyway i want. Apr 20, 2014 at 14:25
  • OK. In that case do it with input faking. Good luck. Apr 20, 2014 at 14:30

1 Answer 1

0

I had a similar problem with drag and drop, although my results were consistent in that the mouse was moving but not the item being dragged.

Your MoveMouse function is missing the MOUSEEVENTF_LEFTDOWN flag. Try input->mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;

Without the flag setting you are just asking the mouse to move without the left mouse button being down.

Edit

I just checked my implementation and I don't set coordinates when pressing or releasing the mouse buttons. Only when moving.

As for variance in results, you may stop these by zeroing out the INPUT before setting fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.