GetFocus

Here is an example of how to get the handle of the window, that currently has the keyboard focus.
Since GetFocus is limited to the calling thread, this will attatch the thread input temporarily.

Private Declare Function apiGetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Int32
Private Declare Function apiGetFocus Lib "user32" Alias "GetFocus" () As Int32
Private Declare Function apiGetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Int32
Private Declare Function apiGetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Int32, ByVal lpdwProcessId As Int32) As Int32
Private Declare Function apiAttachThreadInput Lib "user32" Alias "AttachThreadInput" (ByVal idAttach As Int32, ByVal idAttachTo As Int32, ByVal fAttach As Int32) As Int32

Public Function GetWindowFocus() As Int32
On Error Resume Next
Dim hwnd As Int32, cwnd As Int32
Sendkeys.Flush()
cwnd = apiGetFocus
If cwnd <> 0 Then Return cwnd
hwnd = apiGetForegroundWindow()
If apiAttachThreadInput(apiGetWindowThreadProcessId(hwnd, 0), apiGetCurrentThreadId, 1) <> 0 Then
cwnd = apiGetFocus
apiAttachThreadInput(apiGetWindowThreadProcessId(hwnd, 0), apiGetCurrentThreadId, 0)
End If
Return cwnd
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.Sleep(3000) ' place focus on a child window during these 3 seconds
MessageBox.Show(GetWindowFocus.ToString) 'The handle of the window with focus
End Sub
[1796 byte] By [TT-n] at [2007-11-20 11:56:05]