?
Solved

Using C# how do I send keystrokes to an application

Posted on 2008-10-07
10
Medium Priority
?
1,181 Views
Last Modified: 2013-12-17
I have launched an app via System.Diagnostics.Process.Start.
How do I send keystrokes to that app (In VB6 I would have used SendKeys).
0
Comment
Question by:kev1970
10 Comments
 
LVL 14

Assisted Solution

by:Binuth
Binuth earned 600 total points
ID: 22657973
use SendKeys.Send() method

SendKeys.Send("a")
0
 

Author Comment

by:kev1970
ID: 22657992
Thanks. How do I send the arrow keys?
0
 
LVL 14

Assisted Solution

by:Binuth
Binuth earned 600 total points
ID: 22658022
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:kev1970
ID: 22658102
I'm trying to:
Launch the app
Move to the 4th row of the listbox
Press enter

So far I have launched the app sucessfully but SendKeys.SendWait("{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}"); does not do anything.
How do I ensure that the app has the focus when I send the keystrokes?
0
 
LVL 7

Expert Comment

by:nsanga
ID: 22658306
I think the listbox is not active when you are sending the arrow key.......

In case of manual operation, you will select the listbox ( whcih makes it focused ) and you use arrows to move down.....you have to produce the same sequence.....whats missing is making listbox focused...

I think you need to set the tab stop for the list box and use tab keys to make listbox focused and then use arrow keys........
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 300 total points
ID: 22658627
the following links might provide some useful insite to solve the problem
http://bytes.com/forum/thread629545.html
VB Sample http://www.thescarms.com/dotnet/Process.aspx
0
 

Author Comment

by:kev1970
ID: 22658644
Normally, I double-click the app then use the arrow keys then hit the enter key. In other words, the listbox has focus as soon as the app starts.
How do I ensure that the app has the focus when I send the keystrokes?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 total points
ID: 22659841
After you start the Process, use WaitForInputIdle() to make sure it is ready to receive input...
http://msdn.microsoft.com/en-us/library/ms687022(VS.85).aspx

   Process p = Process.Start(@"c:\someFile.exe");
   p.WaitForInputIdle();
   SendKeys.SendWait("{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}");

Does that help?
0
 

Author Comment

by:kev1970
ID: 22719627
Sorry for the delay.
I also used the following code to focus the (called) app.:

bool ret = SetWindowPos((int)p.Handle, HWND_TOPMOST, 0, 0, 0, 0, (uint)(SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE))
0
 

Author Closing Comment

by:kev1970
ID: 31503765
Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question