Reputation: 1397
I'm trying to start an application an initiate a save by sending Ctrl m + Ctrl a then Alt Gr O to confirm a dialog.
Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
myProcess.StartInfo.FileName = Application.StartupPath & "another.exe"
myProcess.StartInfo.Arguments = " /Args"
myProcess.Start()
Thread.Sleep(5000)
System.Windows.Forms.SendKeys.Send("^%(M)^%(A)")
System.Windows.Forms.SendKeys.Send("%(O)")
I'm then forcing the application to minimise:
ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)
This works great if the window is active and focused, I would much prefer this process to be done silently (It spawns it's own progress dialog) but SendKeys requires an active\focused window.
I think SendMessage is going to be the answer but I cannot see any way of sending the required key combinations?
Upvotes: 0
Views: 1745
Reputation: 9193
SendKeys isn't the most reliable, but you can try to use the AppActivate Function to give focus to the target window/application.
Upvotes: 0
Reputation: 942000
You can't reliably use SendMessage() to send keystrokes. You can't control the process' keyboard state, the state of the Alt, Ctrl and Shift keys. SetKeyboardState() only works for your program, not the one you're trying to control. SendInput is required, which is what SendKeys uses. Which in turn requires the program's main window to have the focus.
Put another way, if there was a better method to fake keyboard input then SendKeys would already be using it. There's a programmer somewhere trying to put his kids through college that can give you a real solution since he knows the program. All you gotta do is find him.
Upvotes: 2