Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Excuse me for my bad english level, i'm french student

I develop a c++ application for move and clic automatically on application's buttons with my cursor who can't support command line, but GUI only.

With windows.h, I tested this:
C#
SetCursorPos(X,Y);
Sleep(500);
Mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(500);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);


This code of my console application work only if is in foreground ( focused )
So if my console application has not focus, my mouse can't move..

Are you solution for resolve this?


Best regards
Sébastien FAVIER
Posted
Updated 22-May-15 4:32am
v2
Comments
Sergey Alexandrovich Kryukov 22-May-15 12:45pm    
Why are you doing all that?
—SA

Mouse has nothing to do with focus. Use the function SendInput:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

The function is low-level; it works as the raw mouse and keyboad events came from the device driver.
Don't use the functions mouse_event and keybd_event; they have been superseded with SendInput.

—SA
 
Share this answer
 
Comments
Sébastien FAVIER 26-May-15 4:07am    
This work perfectly! I tested with Microsoft Paint, TeamViewer, Calc.exe, explorer.exe and other..
But, there are a strange problem with some application as CCleaner, ADwcleaner, MalwareBytes, this not work, he don't focus and click.
(There are a hook key on this applications for block my inputs?)

Thank
Sebastien FAVIER
Sergey Alexandrovich Kryukov 26-May-15 8:46am    
Sure. And if it did not work in some cases, it means that your implementation was not 100% accurate. You need to reproduce all the sequence of events happening when a human user uses the mouse. It should be pretty easy.
—SA
Sébastien FAVIER 26-May-15 9:57am    
Okay, I see :-)

Keep the idea that I must reproduce a human sequence, for begin, I test this
( I forget the website source :/ )
================================================
INPUT Input={0};
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1, &Input, sizeof(INPUT));

/* /!\ I must Sleep some ms?? /!\ */

// left up
::ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1, &Input, sizeof(INPUT));
================================================

The first "Clic" MOUSEEVENTF_LEFTDOWN (0x02) do not give window focus or clic in a button for some applications that I've quoted.

I must reproduce all the sequence of humain events, but with SendInput(), how I can do this?

Thank
Sébastien FAVIER
Sébastien FAVIER 26-May-15 11:43am    
I found my problem !! My application must to have more privileges, and this work

Thank you Sergey :-)
Best regards
Sergey Alexandrovich Kryukov 26-May-15 12:11pm    
You are welcome.
—SA
The mouse belongs the foreground windows, so changes work only there. You can track the mouse with TrackMouseEvent function. For every tracking cycle you must call it again!!!

To get informed when your windows is activated you must handle the WM_ACTIVATE message.
 
Share this answer
 
Thank you for your reply! :-)
I will test this, I keep you informed

Best regards
Sébastien FAVIER
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900