Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hello everyone I hope someone can help me with my question. So i started researching on moving and clicking the mouse within delphi, From what i remember people used to use Mouse_Events but that is now replaced by SendInput, And SendInput just seems so complicated compared to the old way. On top of that i cannot find an good straightfoward examples on how to use it! So could someone please help me out and tell me how to use SendInput to move and click the mouse? Also i have another question directly related to the above.

Im having a hard time figuring out how to simulate mouse clicks and such in a window that is minimized or not currently active. In case it not clear, I want my mouse to be able to click and stuff without actually using the mouse.

For example, while I'm browsing the web I would like my program to be able to be clicking inside of another application at the same time.
Posted
Comments
Sergey Alexandrovich Kryukov 4-Apr-14 2:06am    
If such an elementary technique as SendInput seems "so complicated" to you, perhaps you should pick some easier problems to solve. :-)

Why do you want to simulate input? If this is just for UI development, it means the greatest abuse. Such simulation can only be used in some very specific "system" problems, such as playing keyboard/mouse macro, virtual keyboard, something like that.

—SA
DatSik 4-Apr-14 3:10am    
I didnt mean it was very complicating, Just that it seems like a big mess compared to Mouse_events is all. And i cannot find any good examples on using it. I really dont wanna dive into specifics of what im doing. Im asking for some good examples on moving the mouse via SendInput. Thank you
Sergey Alexandrovich Kryukov 4-Apr-14 11:23am    
No. Just use the MSDN and go ahead. Who told you that you should do programming by copying some other code? "I really don't wanna..." is irrelevant. "Don't wanna" — give up, say yourself, "I am not a software developer".
—SA
Sergey Alexandrovich Kryukov 4-Apr-14 12:59pm    
Please stop it. Instead, write the code according to your goals, and, if you face some problems and can clearly explain them, show what have you done and ask some questions. Then we will gladly help you. No one will help you if you are just whining.
—SA
DatSik 4-Apr-14 14:41pm    
I never what whining? I simply asked for a clear example? Thats all, I searched this site up and down with no luck..This is what I have so far and i wanted to compare what i have to other examples to make sure im in the clear.
var
Inputs: array[0..2] of TInput;
begin
Input.Type := INPUT_MOUSE;
Input.mi.dx := XCoordinate;
Input.mi.dy := YCoordinate;
Input.mi.mouseData := 0;
Input.mi.dwFlags := MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE;
Input.mi.time := 0;
Input.mi.dwExtraInfo := 0;

Input.Type := INPUT_MOUSE;
Input.mi.dx := 0;
Input.mi.dy := 0;
Input.mi.mouseData := 0;
Input.mi.dwFlags := MOUSEEVENTF_LEFTDOWN;
Input.mi.time := 0;
Input.mi.dwExtraInfo := 0;

Input.Type := INPUT_MOUSE;
Input.mi.dx := 0;
Input.mi.dy := 0;
Input.mi.mouseData := 0;
Input.mi.dwFlags := MOUSEEVENTF_LEFTUP;
Input.mi.time := 0;
Input.mi.dwExtraInfo := 0;

SendInput(Length(Inputs), Inputs[0], SizeOf(TInput));
end;

Or

var
Input: TInput;
begin
Input.Type := INPUT_MOUSE;
Input.mi.dx := XCoordinate;
Input.mi.dy := YCoordinate;
Input.mi.mouseData := 0;
Input.mi.dwFlags := MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE or MOUSEEVENTF_LEFTDOWN
or MOUSEEVENTF_LEFTUP;
Input.mi.time := 0;
Input.mi.dwExtraInfo := 0;

SendInput(1, Input, SizeOf(TInput));
end;

1 solution

Thank you for showing the code.

In your first fragment, the problem is: your values of XCoordinate and YCoordinate are lost, you later overwrite these values with 0. Under the debugger, check up these values at the point of assignment, and also at the point of calling SendInput.

In the second fragment, the problem is dwFlags. Try one thing at a time. You cannot set left mouse button down and up at the same time. You many need 2 or 3 separate calls to SendInput. For example, first call moves mouse, another presses a button down, another one releases this button.

Good luck.
—SA
 
Share this answer
 
v2
Comments
DatSik 4-Apr-14 19:56pm    
Awesome, thank you for the input. I will tweak my code a bit and see what happens! Thanks again. I hope this helps anyone else with the same issue
Sergey Alexandrovich Kryukov 4-Apr-14 20:59pm    
Great. After all, it will work. Don't forget to accept the answer formally (green "Accept" button). In all cases, your follow-up questions will be welcome.
—SA

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