Link to home
Start Free TrialLog in
Avatar of matrix_aash
matrix_aashFlag for United Kingdom of Great Britain and Northern Ireland

asked on

If application is active

Hi

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!

Thanks!
Avatar of PaulHews
PaulHews
Flag of Canada image

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")
Avatar of matrix_aash

ASKER

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?
And why is AppActivate not appropriate rather than checking to see if the window is active?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice.  :)
Thanks mate!