模块中:
Public Const WH_KEYBOARD_LL = 13& 'enables monitoring of keyboard
'input events about to be posted
'in a thread input queue
Public Const HC_ACTION = 0& 'wParam and lParam parameters
'contain information about a
'keyboard message
Public Const LLKHF_EXTENDED = &H1& 'test the extended-key flag
Public Const LLKHF_INJECTED = &H10& 'test the event-injected flag
Public Const LLKHF_ALTDOWN = &H20& 'test the context code
Public Const LLKHF_UP = &H80& 'test the transition-state flag
Public Const VK_TAB = &H9 'virtual key constants
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Type KBDLLHOOKSTRUCT
vkCode As Long 'a virtual-key code in the range 1 to 254
scanCode As Long 'hardware scan code for the key
flags As Long 'specifies the extended-key flag,
'event-injected flag, context code,
'and transition-state flag
time As Long 'time stamp for this message
dwExtraInfo As Long 'extra info associated with the message
End Type
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal cb As Long)
Public Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Public m_hDllKbdHook As Long 'public variable holding
'the handle to the hook procedure
Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Static kbdllhs As KBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
'Ctrl+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) Then
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_ESCAPE
'Alt+Tab --------------
If (kbdllhs.vkCode = VK_TAB) And CBool(kbdllhs.flags And LLKHF_ALTDOWN) Then
'Debug.Print "Alt+Tab blocked"
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_TAB
'Alt+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And CBool(kbdllhs.flags And LLKHF_ALTDOWN) Then
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_ESCAPE
'If (kbdllhs.vkCode = 91) And CBool(GetKeyState(91) < 0) Then
' LowLevelKeyboardProc = 1
' Exit Function
'End If