?
Solved

Using C# how do I send keystrokes to an application

Posted on 2008-10-07
10
Medium Priority
?
1,174 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 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
Industry Leaders: 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 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

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…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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