Solved

Using C# how do I send keystrokes to an application

Posted on 2008-10-07
10
1,165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 86

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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