How Can I Activate an App from Another App and Send Key Strokes

Posted on 2006-04-21
Last Modified: 2012-08-13
I have 2 separate applications running. From One application I need to activate (bring to front) the other application and send some keyboard keys to simulate the used pressing those keys.
The application that will activate the other is on VB.NET VS2005
Question by:ofern01
    LVL 6

    Accepted Solution

    To activate another app:  store the window handle of the other application and then use BringWindowToTop to activate the app.

    Also, look at this:
    LVL 1

    Author Comment

    I do not call the other app from my VB program. It is already running when I exec my App. So i will need a way to find that handle from my VB program.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    You can use code like this to get a handle to the other apps window and Send keystrokes:

        Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As IntPtr) As Integer

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim ps() As Process = Process.GetProcessesByName("notepad")
            If ps.Length > 0 Then
                Dim p As Process = ps(0)
                Dim hWnd As IntPtr = p.MainWindowHandle
                SendKeys.Send("Hello World!")
            End If
        End Sub

    Notice that in GetProcessesByName(), we pass in the name of the Exectuable without any path and without the ".exe" extension.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( 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…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now