Can I send mouse clicks to another application?
-
Hi everybody! First post here, I have a question which might be simple but who knows... ;) You know about the SendKeys class, right? It lets you send keystrokes to another Windows application. Well, my question is, is there anything similar which will let me send mouse clicks to another application? In case you're wondering why I want to do this, no, I am not planning on writing any trojan horse software! ;) I'm just going to write a sort of plugin for a strategy game I have which would replace the starship design module of the game - except the game doesn't support plugins, so I'm going to do it as an external program, and I want to be able to save the user a lot of repetitive clicking - in Space Empires IV, you have to click 750 times to add 750 "plasma projection armors" to your ship (should the mod you're playing have components small enough to fit 750 of on a ship!), but in my program you would just type in 750 and it would do the clicking for you! :D Thanks in advance for any help!
-
Hi everybody! First post here, I have a question which might be simple but who knows... ;) You know about the SendKeys class, right? It lets you send keystrokes to another Windows application. Well, my question is, is there anything similar which will let me send mouse clicks to another application? In case you're wondering why I want to do this, no, I am not planning on writing any trojan horse software! ;) I'm just going to write a sort of plugin for a strategy game I have which would replace the starship design module of the game - except the game doesn't support plugins, so I'm going to do it as an external program, and I want to be able to save the user a lot of repetitive clicking - in Space Empires IV, you have to click 750 times to add 750 "plasma projection armors" to your ship (should the mod you're playing have components small enough to fit 750 of on a ship!), but in my program you would just type in 750 and it would do the clicking for you! :D Thanks in advance for any help!
This has been asked many times in this forum. The answer is to P/Invoke
SendInput
. See http://pinvoke.net/default.aspx/user32.SendInput[^] for the method signature. This sends the input to the foreground window. If you have simple input you want to send (keyboard input only), you can use theSendKeys
class, which also sends input to the foreground window. This means that your application should switch the other application to the foreground before sending input. There are many ways of doing this, such as getting the processes usingProcess.GetProcessesByName
, getting theMainWindowHandle
, and pass that to a P/Invoke'dSetForegroundWindow
(see http://www.pinvoke.net/default.aspx/user32.SetForegroundWindow[^]). There are other ways but these deal with Windows messaging, something you should understand before trying to use. You can learn more about Windows messaging in the Platform SDK in the MSDN Library[^].Microsoft MVP, Visual C# My Articles
-
This has been asked many times in this forum. The answer is to P/Invoke
SendInput
. See http://pinvoke.net/default.aspx/user32.SendInput[^] for the method signature. This sends the input to the foreground window. If you have simple input you want to send (keyboard input only), you can use theSendKeys
class, which also sends input to the foreground window. This means that your application should switch the other application to the foreground before sending input. There are many ways of doing this, such as getting the processes usingProcess.GetProcessesByName
, getting theMainWindowHandle
, and pass that to a P/Invoke'dSetForegroundWindow
(see http://www.pinvoke.net/default.aspx/user32.SetForegroundWindow[^]). There are other ways but these deal with Windows messaging, something you should understand before trying to use. You can learn more about Windows messaging in the Platform SDK in the MSDN Library[^].Microsoft MVP, Visual C# My Articles
-
Thanks! Now I've heard bad things about PlatformInvoke... I know for one it's platform-specific, of course, but is there anything else I should be aware of before I go using it willy-nilly?
Bad things? Besides not being portable code... What bad things? RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
-
Thanks! Now I've heard bad things about PlatformInvoke... I know for one it's platform-specific, of course, but is there anything else I should be aware of before I go using it willy-nilly?
P/Invoke is used throughout the entire .NET Framework Class Library (FCL). Almost all the Windows Forms controls, for example, "simply" encapsulate the Windows Common Controls. They P/Invoke many native methods, send and handle messages, etc. Many other classes in the FCL P/Invoke native functionality. Sending input is platform-specific anyway.
Microsoft MVP, Visual C# My Articles