Results 1 to 6 of 6

Thread: [RESOLVED] Check which Window is in focus

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Resolved [RESOLVED] Check which Window is in focus

    How do I check if a Window is in Focus? Like, I want to put in a Process Name, and see if its the Window in focus.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Check which Window is in focus

    Wouldn't your window be the one in focus then?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Check which Window is in focus

    Quote Originally Posted by kleinma View Post
    Wouldn't your window be the one in focus then?
    No, I would use GetAsyncKeyState.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Check which Window is in focus

    How would that make your window not the focused one?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Check which Window is in focus

    Quote Originally Posted by kleinma View Post
    How would that make your window not the focused one?
    I want it to do something like:
    Code:
    If GetAsyncKeyState(Keys.Insert) Then
    Dim Proc as Process() = Process.GetProcessesByName("notepad")
    If WindowIsInFocus(Proc) Then
    MsgBox("Notepad is in focus")
    End If
    End If

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

    Re: Check which Window is in focus

    You can use the GetForegroundWindow API to get the window that is currently in focus, but I'm not sure about your method of triggering your code to be run when not in focus - I think the RegisterHotKey API might be a better way of doing it because as I see it you are going to have to constantly check that GetAsyncKeyState function every second or so (which is never a good way of doing something).

    Here's an example of how to declare the GetForegroundWindow API:
    vb Code:
    1. ''' <summary>
    2. ''' Gets the Hwnd of the currently active window
    3. ''' </summary>
    4. <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="GetForegroundWindow")>  _
    5. Public Shared Function GetForegroundWindow() As System.IntPtr
    6. End Function

    and RegisterHotKey:
    vb Code:
    1. 'Constants
    2. Public Const WM_HOTKEY As Integer = &H312
    3.     Public Const MOD_ALT As Integer = 1
    4.     Public Const MOD_CONTROL As Integer = 2
    5.     Public Const VK_CONTROL As Integer = &H11
    6.  
    7. ''' <summary>
    8.     ''' Registers a system wide hotkey
    9.     ''' </summary>
    10.     ''' <param name="hWnd">The Hwnd of the window that should receive a notification when the hotkey is pressed</param>
    11.     ''' <param name="id">A unique ID for the hotkey</param>
    12.     ''' <param name="fsModifiers">The keys that must be pressed when vk is pressed to trigger the hotkey</param>
    13.     ''' <param name="vk">The key that must be pressed when the keys in fsModifiers are pressed to trigger the hotkey</param>
    14.     Public Declare Function RegisterHotKey Lib "user32" Alias "RegisterHotKey" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As UInteger, ByVal vk As UInteger) As Boolean
    15.  
    16.     ''' <summary>
    17.     ''' Removes a system wide hotkey
    18.     ''' </summary>
    19.     ''' <param name="hWnd">The Hwnd of the window that the hotkey was registered against</param>
    20.     ''' <param name="id">The ID of the hotkey to remove</param>
    21.     Public Declare Function UnregisterHotKey Lib "user32" Alias "UnregisterHotKey" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
    RegisterHotKey is a bit more involved than just calling a method though, you have to override the window message processing event for your form to capture the message that is sent when the hotkey is pressed, like so:
    vb.net Code:
    1. '<<< Override the message processing routine to capture the WM_HOTKEY message >>>
    2.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    3.         If m.Msg = WM_HOTKEY Then
    4.             If m.WParam.ToString = "6003" Then '<-- change this number to match the ID you gave the hotkey when you called RegisterHotKey
    5.                'Do stuff here that you want to happen when your hotkey is pressed
    6.             End If
    7.         End If
    8.         MyBase.WndProc(m)
    9.     End Sub
    Last edited by chris128; Apr 24th, 2010 at 07:47 PM.
    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