Solved

Send Keys to another application without activating it

Posted on 2011-03-25
2
936 Views
Last Modified: 2012-05-11
Hi

I am writing a WPF application using VB .NET and am trying to find sample code for sending (if possible) keystrokes (sometimes single characters, sometimes whole strings and sometimes control keystrokes such as ENTER) to another application.

We have a legacy application that does not expose an API for automation/interop and as such until we redevelop, have to hack it by sending keystrokes.  However, we would prefer to avoid using AppActivate seeing that we cannot guarantee the popping up of the application at random intervals will not cause other issues when someone is using the computer.

So in short, is it possible to send (complex) keystrokes to another application without activating it using VB .NET and if so, how?

Thanks
Vyyk
0
Comment
Question by:Vyyk_Drago
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35215804
Yes...but you have to use windows APIs to achieve it.

Basically you need a handle to the window you want to send to (like a TextBox).  Then you use SendMessage() to either simulate individual keystrokes or to set the text outright.  We can't tell you the exact correct combination as every application is different and may need one approach or another to make it "behave" correctly.  Invoking menus gets a little tricky as well as the window that processes those types of messages sometimes is not even visible and isn't obvious.

Tools such as Spy++ can be very useful in figuring out the window hierarchy and structure of the external app.

To get the main window handle you can use something like Process.GetProcessesByName().

To get child windows you can use these APIs alone, or in combination:
EnumWindows(), EnumChildWindows()
FindWindow(), FindWindowEx()
GetWindow(), GetNextWindow()

To inject the info use SendMessage().

Look these up on the MSDN website for more info and then do a web search.
0
 

Author Closing Comment

by:Vyyk_Drago
ID: 35240600
Thanks, very helpful.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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