User Tools

Site Tools


automating_20mouse_20actions

Automating mouse actions

by Richard Russell, October 2008

You may on occasion want to automate mouse actions, such as simulating the effect of the user clicking a button or rotating the wheel. One use for such a facility could, for example, be in a training or tutorial situation where you wish to illustrate how to use the mouse to achieve a particular result.

Windows provides the means to do that by means of the SendInput API (Windows 98 and later only). The following code has the same effect as the user double-clicking the left mouse button:

        _INPUT_MOUSE = 0
        _MOUSEEVENTF_LEFTDOWN = 2
        _MOUSEEVENTF_LEFTUP = 4
 
        DIM Input{type%, Mouse{dx%, dy%, mouseData%, dwFlags%, time%, dwExtraInfo%}}
        Input.type% = _INPUT_MOUSE
 
        Input.Mouse.dwFlags% = _MOUSEEVENTF_LEFTDOWN
        SYS "SendInput", 1, Input{}, DIM(Input{})
        Input.Mouse.dwFlags% = _MOUSEEVENTF_LEFTUP
        SYS "SendInput", 1, Input{}, DIM(Input{})
        Input.Mouse.dwFlags% = _MOUSEEVENTF_LEFTDOWN
        SYS "SendInput", 1, Input{}, DIM(Input{})
        Input.Mouse.dwFlags% = _MOUSEEVENTF_LEFTUP
        SYS "SendInput", 1, Input{}, DIM(Input{})

Other constants that you may need are as follows:

        _MOUSEEVENTF_RIGHTDOWN = &8
        _MOUSEEVENTF_RIGHTUP = &10
        _MOUSEEVENTF_MIDDLEDOWN = &20
        _MOUSEEVENTF_MIDDLEUP = &40
        _MOUSEEVENTF_WHEEL = &800

In the case of wheel movements the mouseData% member of the structure must be loaded with a positive value for rotations away from the user, and a negative value for rotations towards the user. A value of 120 corresponds to one 'click':

        _INPUT_MOUSE = 0
        _MOUSEEVENTF_WHEEL = &800
 
        DIM Input{type%, Mouse{dx%, dy%, mouseData%, dwFlags%, time%, dwExtraInfo%}}
        Input.type% = _INPUT_MOUSE
 
        Input.Mouse.dwFlags% = _MOUSEEVENTF_WHEEL
        Input.Mouse.mouseData% = 120
        SYS "SendInput", 1, Input{}, DIM(Input{})

The SendInput API provides many other facilities, such as simulating keyboard input (you can use it as an alternative to the method described at Faking keyboard input). For full details see the relevant Microsoft documentation.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
automating_20mouse_20actions.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1