System.Diagnostics.Process exposes a StreamWriter named StandardInput, which accepts only characters as far as I know.
But I need to send keystrokes as well, and some keystrokes don't map well to characters.
What should I do?
System.Diagnostics.Process exposes a StreamWriter named StandardInput, which accepts only characters as far as I know.
But I need to send keystrokes as well, and some keystrokes don't map well to characters.
What should I do?
If you have a Windows Forms window that you can send the keys to, then SendKeys might be an appropriate solution.
For pressing backspace and Ctrl+C, that should be
There's an input Simulator found here on Codeplex which may do just the job for you. I am working on a sample code and will post back here shortly, bear in mind the Input Simulator is similar to what was found in the link supplied by Remus...
Edit: I have found that there is a limitation with this, you can definitely get away with the typical
System.Windows.Forms.SendKeys.Send
method, it does work effectively!, but, the process must haveIn your case, it's a matter of finding the window, set it active via pinvoke 'SetForegroundWindow', and send the sequences
^{BREAK}
which sends the Ctrl+Break signal to the process which does work very well (especially if the process is a command line program/batch file). Here's an article on CodeProject that does this exactly and mirrors the SendKeys...I have yet to paste some code into this to demonstrate ....Edit#2: Actually I am quite surprised...as this code will show (proof of concept)...it is using:
FindWindow
API call, because I knew the window's title was and was somehow, able to bring it to the foreground, and using the InputSimulator to send the keystrokes to it...or use the traditional plain oldSendKeys
function...the reason I had theThread.Sleep
is to ensure that the keystrokes are sent in order to be 'pushed into the keyboard queue of the "active foreground window", which despite that, is hidden'Nitpicky aside, I know that the
netStat
is running off the 'BackgroundWorker' thread, and I directly invoked the 'PostCtrlC' method from the main GUI thread...this is pedantic as a proof-of-concept code, but it does show that it needs to implement 'ISynchronizeInvoke' to make it thread-safe, that aside...it does indeed work.You are mixing input streams with control signals. A console process has a default input stream which you can control with the StandardInput, as you already know. But Ctrl-C and Ctrl-Break are not characters sent to the process through this stream, but instead they are instead control signals that the process receives using the registered signal handlers, see CTRL+C and CTRL+BREAK Signals:
To send fake signals to a process you can use
GenerateConsoleCtrlEvent
and send eitherCTRL_C_EVENT
orCTRL_BREAK_EVENT
. This API has no .Net equivalent, so you have to PInvoke it.To use it from .NET you simply need to include the function definition:
Have you seen this great tool - AutoIt. This is a scripting tool. To send a backspace you would use
Send("{BACKSPACE}")
This is a great tool and it can help in automating many manual clicks/double-clicks/etc.
Is this relevant to your question ?