'*************************************************************************
'**模 块 名:拦截Alt+Tab,Alt+F4,Ctrl+Esc
'**日 期:2004年05月16日
'**描 述:载入时用 Hook(), 卸载时用UnHook()
'*************************************************************************
'在WIN2K中,执行下面的程序中的Hook(),可以拦截Alt+Tab,Alt+F4,Ctrl+Esc
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Private 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
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const HC_ACTION = 0
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const VK_TAB = &H9
Private Const VK_ESCAPE = &H1B
Private Const VK_CONTROL = &H11
Private Const WH_KEYBOARD_LL = 13
Private Const LLKHF_ALTDOWN = &H20
Private PrevHook As Long
Public Sub Hook()
PrevHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
End Sub
Public Sub UnHook()
UnhookWindowsHookEx PrevHook
End Sub
Public Function LowLevelKeyboardProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean
Dim p As PKBDLLHOOKSTRUCT
If ncode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP
CopyMemory p, ByVal lParam, Len(p)
If (p.flags And LLKHF_ALTDOWN) <> 0 Then 'Alt + AnyKey
fEatKeystroke = True
End If
If (p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0) Then 'Ctrl + Esc
fEatKeystroke = True
End If
Case Else
'do nothing
End Select
End If
If fEatKeystroke Then
LowLevelKeyboardProc = 1
Else
CallNextHookEx WH_KEYBOARD_LL, ncode, wParam, lParam
End If
End Function