mousemove

Nov 27, 2014 at 6:04pm

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!
Nov 27, 2014 at 6:14pm
Nov 27, 2014 at 6:18pm
umm, hi that didn't really help me, can you please give some code as an example, thanks!
Nov 27, 2014 at 6:30pm
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 Nov 27, 2014 at 6:33pm
Nov 27, 2014 at 6:37pm
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
Nov 27, 2014 at 6:39pm
Did you include windows.h?
Nov 27, 2014 at 6:41pm
yes I just copied your code and pasted it in my codeblocks, but it doesn't work even with your second code
Nov 27, 2014 at 6:49pm
Looks your MinGW version does not work properly.
try to add one define before inclusion:
1
2
#define _WIN32_WINNT 0x0500
#include "Windows.h" 
Nov 27, 2014 at 6:52pm
thank you it worked now, I have one more question
how to simulate a click :)
Nov 27, 2014 at 7:00pm
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 Nov 27, 2014 at 7:07pm
Nov 27, 2014 at 7:05pm
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)'
Nov 27, 2014 at 7:06pm
my compiler is:

=== TDM-GCC Compiler Suite for Windows ===
--- GCC 4.6 & 4.7 Series ---
*** Standard MinGW 32-bit Edition ***
Nov 27, 2014 at 7:07pm
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 Nov 27, 2014 at 7:08pm
Nov 27, 2014 at 7:13pm
thank you!
You are amazing!!!! :D
Topic archived. No new replies allowed.