Simulate the mouse event implementation ideas and code

  • 2020-04-02 02:06:10
  • OfStack

Simulate mouse events, typically using mouse_event() and SendInPut(). Mouse_event () was gradually replaced by SendInPut() in later versions of Windows. In SendInPut(), which simulates mouse movement, the value of the flag bit is different and the meaning of the input coordinates is different. In simple terms, adding the MOUSEEVENTF_ABSOLUTE flag bit indicates that the mouse is moving through the absolute coordinates, and that the coordinates are being converted. The cursor is divided into 65,535 small blocks on the screen, which can be converted as follows:


double fx = x *(65535.0f / fScreenWidth);
double fy = y *(65535.0f / fScreenHeight);

If the MOUSEEVENTF_ABSOLUTE flag bit is not used, the coordinate is the displacement relative to the previous coordinate.

SendInPut() mouse time USES the following structure:


typedef struct tagMOUSEINPUT {
    LONG    dx;
    LONG    dy;
    DWORD   mouseData;
    DWORD   dwFlags;
    DWORD   time;
    ULONG_PTR dwExtraInfo;
} MOUSEINPUT, *PMOUSEINPUT, FAR* LPMOUSEINPUT;

The full explanation in MSDN is as follows:
        The absolute position of the dx mouse, or the value of the dwFlags member generated from the amount of movement of the last mouse event. The x-coordinate of the mouse is specified as absolute data, relative to the number of moving pixels specified as data.
        The absolute position of the dy mouse, or the value of the dwFlags member generated from the amount of movement of the last mouse event. The y coordinate of the mouse is specified as absolute data, relative to the number of moving pixels specified as data.
        MouseData if dwFlags contains MOUSEEVENTF_WHEEL, mouseData specifies the amount of wheel motion. Positive values indicate that the wheel is rotating forward, that is, away from the user, and negative values indicate that the wheel is rotating backward, that is, toward the user. The wheel is defined as WHEEL_DELTA, which is 120.

        The following code demonstrates several specific mouse operations.
        1. How can I simulate mouse events in my application?

        Two API functions are available: mouse_event() and SendInput();

        2. Which API function should be used?

        In Windows NT/2000/XP, the mouse_event () function has been replaced by the SendInput () function. Therefore, on these operating systems, you should use the SendInput () function. Unless you need to provide backwards compatibility with Windows98 etc.

        3. How to use the SendInput () function to simulate clicking the left mouse button?


void LeftClick ( )
{  
    INPUT    Input={0};
    //Under the left?
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
    ::SendInput(1,&Input,sizeof(INPUT));
    //Left-click to lift
    ::ZeroMemory(&Input,sizeof(INPUT));
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
    ::SendInput(1,&Input,sizeof(INPUT));
}

4. How to use the SendInput () function to simulate right-clicking?


void RightClick ( )
{  
    INPUT    Input={0};
    //Right click to press the
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_RIGHTDOWN;
    ::SendInput(1,&Input,sizeof(INPUT));
    //Right-click to lift
    ::ZeroMemory(&Input,sizeof(INPUT));
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_RIGHTUP;
    ::SendInput(1,&Input,sizeof(INPUT));
}

5. How to use the SendInput () function to simulate mouse movement?


void MouseMove (int x, int y )
{  
    double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
    double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
    double fx = x*(65535.0f/fScreenWidth);
    double fy = y*(65535.0f/fScreenHeight);
    INPUT  Input={0};
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
    Input.mi.dx = fx;
    Input.mi.dy = fy;
    ::SendInput(1,&Input,sizeof(INPUT));
}

6. How to use the SendInput () function to simulate clicking the middle mouse button?


void MiddleClick()
{
    INPUT    Input={0};
    //Set the roller volume
    Input.type      = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_WHEEL;
    Input.mi.mouseData = 500;
    ::SendInput(1,&Input,sizeof(INPUT));
}


Related articles: