Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I was wondering whether it's possible in C# (or alternatively in any other programming language) to raise click event programmatically, thus effectively simulating user pressing left mouse button.
I know that I can just call appropriate event handler, based on cursor position, but that can only simulate user clicking inside windows of my application. What I would like to be able to do is to simulate user clicks in windows of another program, thus forcing the other program to react in the same way as if user actually clicked on it.

Thanks in advance.
Posted
Updated 20-Jun-11 3:53am
v2

Microsoft provides something called Microsoft Active Accessibility[^] to allow you to automate applications. Silverlight and WPF applications also provide something called Automation Peers[^] to allow you to automate applications.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Jun-11 10:42am    
Interesting links! 5+
Using P/Invoke you can send messages to other windows, including WM_MOUSEDOWN/UP and WM_CLICK. SendInput[^] also appears to be useful.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Jun-11 10:41am    
Good point! 5+
dsnlkc 22-Jun-11 12:20pm    
Thank you for your suggestion. I read about SendInput at MSDN and it seams like nice solution to my problem. However, I couldn't find instruction how to call it in my C# code, which class and namespace does it belong to, whether it is static method or not, and which reference and using directive should I add to my C# project.

Can you please answer these questions for me?
Also, if it's not much trouble, small example would be nice.

Thanks again
BobJanova 22-Jun-11 13:25pm    
It's a Win API function, you will have to use P/Invoke to use it.
dsnlkc 1-Jul-11 3:13am    
SendInput function does the trick. A little walkthrough for using it is in order, though. It might be of some help for future readers.

First of all, as BobJanova said, it is a Win API function, not .net method. On this link

http://pinvoke.net/default.aspx/user32.SendInput

you can find detail description (tutorial) about using it. You can find open source C# wrapper for this function here:

http://inputsimulator.codeplex.com/

Just add this project to your solution and you will be able to simulate keyboard entries. However, I experienced some problems with simulation of mouse clicks, so I had to implement that myself, but data types defined in the project offered me great help. I just had to add one more method to the InputSimulator class.

Thank you BobJanova for the suggestion and your time. Happy coding.
0) To imulate a button click in the app containing the button, use the PeformClick method.

1) If it's in another app, you need to have the window handle of the window that has the button in it, and then use either PostMessage or SendMessage (I would use Postmessage myself). Go to pInvoke.net[^] to get the proper prototypes for these API methods.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Jun-11 10:41am    
Nice link! 5+

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