Use the GetForegroundWindow api function.
Code:
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Sub Command1_Click()
Dim hforewnd As Long
Dim handl As Long
handl = Me.hWnd
hforewnd = GetForegroundWindow()
If handl = hforewnd Then
MsgBox "Your program is on top of all others!"
Else
MsgBox "Your program is not on top of all others!"
End If
End Sub
Of course, your program will always be on top because it has the focus, so you can use the GetAsyncKeyState api function to detect key presses.
Code:
Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
'Detect Tab key
If GetAsyncKeyState(vbKeyTab) Then
Command1_Click
End If
End Sub