mousemove


Hello everyone,
Does anyone know how to simulate mouse move and mouse click using code::blocks as a platform and Windows 7

Thank you in advance!
umm, hi that didn't really help me, can you please give some code as an example, thanks!
Try to control your mouse for 5 seconds:
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

int main()
{
    MOUSEINPUT mouseDown {2, 5, 0, MOUSEEVENTF_MOVE, 0, NULL};
    INPUT mouse {INPUT_MOUSE, mouseDown};
    for(int i = 0; i < 500; ++i) {
        SendInput(1, &mouse, sizeof(mouse));
        Sleep(10);
    }
}

Edit: in case you have an outdated compiler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <windows.h>

int main()
{
    MOUSEINPUT mouseDown;
    mouseDown.dx = 2;
    mouseDown.dy = 5;
    mouseDown.mouseData = 0;
    mouseDown.dwFlags = MOUSEEVENTF_MOVE;
    mouseDown.time = 0;
    mouseDown.dwExtraInfo = NULL;
    INPUT mouse;
    mouse.type = INPUT_MOUSE;
    mouse.mi = mouseDown;
    for(int i = 0; i < 500; ++i) {
        SendInput(1, &mouse, sizeof(mouse));
        Sleep(10);
    }
}
Last edited on
it gives me some errors like:

mousemove.cpp: In function 'int main()':
error: 'MOUSEINPUT' was not declared in this scope
error: expected ';' before 'mouseDown'
error: 'INPUT' was not declared in this scope
error: expected ';' before 'mouse'
error: 'mouse' was not declared in this scope
error: 'SendInput' was not declared in this scope
Did you include windows.h?
yes I just copied your code and pasted it in my codeblocks, but it doesn't work even with your second code
Looks your MinGW version does not work properly.
try to add one define before inclusion:
1
2
#define _WIN32_WINNT 0x0500
#include "Windows.h" 
thank you it worked now, I have one more question
how to simulate a click :)
Well, page on MOUSEINPUT tells that you need to send message with MOUSEEVENTF_LEFTDOWN flag set in dwFlags. Then you need to send message with MOUSEEVENTF_LEFTUP flag set to simulate button release;
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx

1
2
3
4
5
6
7
8
9
10
#define _WIN32_WINNT 0x0500
#include <windows.h>

int main()
{
    MOUSEINPUT click[2] {{0, 0, 0, MOUSEEVENTF_LEFTDOWN, 0, NULL},
                         {0, 0, 0, MOUSEEVENTF_LEFTUP, 0, NULL}};
    INPUT mouse_click[2] {{INPUT_MOUSE, click[0]}, {INPUT_MOUSE, click[1]}};
    SendInput(2, mouse_click, sizeof(mouse_click[0]));
}
Paste this in your IDE, move mouse to the Windows Start button and press F9.
Last edited on
I have a problem again
error: expected '}' before ';' token
error: cannot convert 'INPUT (*)[2] {aka tagINPUT (*)[2]}' to 'LPINPUT {aka tagINPUT*}' for argument '2' to 'UINT SendInput(UINT, LPINPUT, int)'
my compiler is:

=== TDM-GCC Compiler Suite for Windows ===
--- GCC 4.6 & 4.7 Series ---
*** Standard MinGW 32-bit Edition ***
Ah, remove & in the SendInput function. And add another closing bracket at the end of mouse_click definition. I fixed my previous code.
Last edited on
thank you!
You are amazing!!!! :D
Topic archived. No new replies allowed.