If application is active


I am writing a program that sends keys to an application. So i need an if statement that says If notepad is active, run this code. Otherwise it will be sending keys when i'm doing anything!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sendkeys is somewhat unreliable at best.  You can use code like this to activate a window before sending keys.  (If the window is already active, no harm is done.)

Dim ProcessID As Integer = Shell("Notepad.exe", AppWinStyle.NormalFocus)
AppActivate(ProcessID)  'Also takes window text, like "Untitled - Notepad"
SendKeys.Send("This is a test")
matrix_aashAuthor Commented:
This is got an in-game thing so send keys is the only way unless you know the API for the game which is unlikley. Is there an if statement that checks to see if it is activated?
Depends.  Could be done with Win32 API depending on how you are tracking the notepad window... How is notepad being run in the first place?  Does it have a window caption we can track?  Could there be more than one notepad window?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

And why is AppActivate not appropriate rather than checking to see if the window is active?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is how to determine if a NotePad instance currently has the focus:
Public Class Form1
    Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If AppHasFocus("NotePad") Then
            Label1.Text = "NotePad has Focus"
            Label1.Text = "NotePad does NOT have Focus"
        End If
    End Sub
    Private Declare Auto Function GetForegroundWindow Lib "user32.dll" () As IntPtr
    Private Function AppHasFocus(ByVal ExeNameWithoutExtension As String) As Boolean
        Dim activeHandle As IntPtr = GetForegroundWindow()
        For Each p As Process In Process.GetProcessesByName(ExeNameWithoutExtension)
            If p.MainWindowHandle.Equals(activeHandle) Then
                Return True
            End If
        Return False
    End Function
End Class

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nice.  :)
matrix_aashAuthor Commented:
Thanks mate!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.