Results 1 to 7 of 7

Thread: Check if app has focus? (External app)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Check if app has focus? (External app)

    So, I know from the user32 dll, and I set that as focused.

    Is there a easy way of determining if the app has focus?


    im using send keys, and i dont want it typing the keys and such, if the app doesnt have focus.

    Also, if you click off of it, etc... It's more so a pause thing...

    The only other way I can think of, is setting it so,

    If myapp has focus, then pause the keysends.... then if it loses focus, assume the app i want has focus again.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Check if app has focus? (External app)

    you could use the GetActiveWindow API function to return the window handle of the active window, + then compare it to the window handle of the app you're looking for:

    vb Code:
    1. Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow"
    2. () As integer

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Check if app has focus? (External app)

    Quote Originally Posted by .paul. View Post
    you could use the GetActiveWindow API function to return the window handle of the active window, + then compare it to the window handle of the app you're looking for:

    vb Code:
    1. Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow"
    2. () As integer
    That should be an IntPtr not an Integer so that it works on 64 bit systems as well
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: Check if app has focus? (External app)

    i did the above, and it returns a number when MY app is selected, but as soon as my app is NOT selected... (on any other program, its 0)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: Check if app has focus? (External app)

    not sure if you guys got what I meant,

    but im trying to determine if say... Notepad had focus then sendkeys... if anything else has focus, dont.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: Check if app has focus? (External app)

    Ok, I am using 'GetForegroundWindow' which is displaying a different # for each program.. So i'm going to assume thats what I want.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Check if app has focus? (External app)

    Yeah that is the handle for each window, which will be what you want if you are then going to use that in another API call to do something to the window.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width