Solved

Using C# how do I send keystrokes to an application

Posted on 2008-10-07
10
1,160 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 200 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 200 total points
ID: 22658022
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 100 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 85

Accepted Solution

by:
Mike Tomlinson earned 200 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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