Option Explicit
Declare Function SetWindowsHookEx Lib _
"user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long
Const HC_ACTION = 0
Const VK_ALT = &H12
Const VK_SHIFT = &H10
Const WH_KEYBOARD_LL = 13
Dim hhkLowLevelKybd As Long
Dim lngFormHndl As Long
Dim blnHookEnabled As Boolean
Public Function LowLevelKeyboardProc _
(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If (nCode = HC_ACTION) Then
'If the ALT and SHIFT Keys are simultanously pressed then
'show the other UserForm
If (GetKeyState(VK_ALT) And &H8000) And (GetKeyState(VK_SHIFT) _
And &HF0000000) Then
'Check if there is a loaded Form
lngFormHndl = FindWindow("ThunderDFrame", vbNullString)
' If so is it Form1 or Form2 ?...If either Set Focus to it
If lngFormHndl = FindWindow(vbNullString, UserForm1.Caption) Or _
lngFormHndl = FindWindow(vbNullString, UserForm2.Caption) Then
SetFocus lngFormHndl
End If
'Check Which UserForm Is Active and hide it
Select Case GetActiveWindow
Case Is = FindWindow(vbNullString, UserForm1.Caption)
UserForm1.Hide
UserForm2.Show
Case Is = FindWindow(vbNullString, UserForm2.Caption)
UserForm2.Hide
UserForm1.Show
End Select
End If
End If
' Call next Hook if there is one.
LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End Function
Public Sub Hook_KeyBoard()
'Hide XL and VBE
Application.Visible = False
Application.VBE.MainWindow.Visible = False
If blnHookEnabled = False Then
hhkLowLevelKybd = SetWindowsHookEx _
(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, Application.Hinstance, 0)
blnHookEnabled = True
End If
End Sub
Public Sub Unhook_KeyBoard()
'Show XL
Application.Visible = True
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx hhkLowLevelKybd
blnHookEnabled = False
End Sub