[此贴子已经被作者于2007-5-25 10:14:45编辑过]
对于没打过sp1和sp2补丁的xp系统,方法如下:
把以下代码放入vb的模块中:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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 CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_F4 = vbKeyF4
Public Const WH_KEYBOARD_LL = 13
Public Const LLKHF_ALTDOWN = &H20
Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Dim p As KBDLLHOOKSTRUCT
Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fEatKeystroke As Boolean
If (nCode = HC_ACTION) Then
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
CopyMemory p, ByVal lParam, Len(p)
fEatKeystroke = _
((p.vkCode = VK_TAB) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
((p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0)) Or _
((p.vkCode = 91) Or (p.vkCode = 92) Or (p.vkCode = 93)) Or _
((p.vkCode = VK_F4) And (p.flags And LLKHF_ALTDOWN) <> 0) '加入这行代码屏弊Alt+F4
End If
End If
If fEatKeystroke Then
LowLevelKeyboardProc = -1
Else
LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End If
End Function
然后先在窗体的声明中加入:
Dim hhkLowLevelKybd As Long
在窗体的load事件中写上:
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
在窗体的unload事件中写上:
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx hhkLowLevelKybd
以上代码在没有打过sp1和sp2补丁的xp系统中,可以屏蔽掉xp中所有定义的系统热键(包括CTRL+ESC、ALT+TAB、ALT+F4、WIN键+任何键的组合、CTRL+ALT+DEL、等等)
对于打过sp1和sp2补丁的xp系统,以上代码不能屏蔽CTRL+ALT+DEL组合键。但是可以这样:
在窗体中的load事件中加入:
s = Environ("windir")
s = s + "\system32\taskmgr.exe"
Open s For Random Lock Read As #1
在窗体的unload事件中加入:
Close #1
就可以实现屏蔽CTRL+ALT+DEL组合键,再组合上面的代码,就可以屏蔽掉xp系统中所有已经定义的系统热键了。
如果想屏蔽其他键 比如a到z
添加这个代码就可以 (p.vkCode = 93) 修改一下参数 数值是字母对应ASCII值