Solved

Using C# how do I send keystrokes to an application

Posted on 2008-10-07
10
1,155 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now