|
|
Title | Disable certain key combinations such as Alt-Tab |
Description | This example shows how to disable certain key combinations such as Alt-Tab in Visual Basic 6. It uses the SetWindowsHookEx API function to create a low level keyboard hook |
Keywords | disable keys, key combinations, Alt-Tab |
Categories | Tips and Tricks |
|
|
Thanks to Herman Eldering.
Use a low-level keyboard hook WH_KEYBOARD_LL.
This works only for NT 4.0 SP3 and later, and Windows 2000. It doesn't work for Windows 98.
Marco Cox informs me that it works in Windows XP, too.
WARNING: Use this at your own risk. You could conceivably use it to prevent all keyboard input and really mess up your system.
When you check or uncheck the program's check box, the following code installs or uninstalls the keyboard hook.
|
|
Private Sub chkDisable_Click()
If chkDisable = vbChecked Then
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, App.hInstance, _
0)
Else
UnhookWindowsHookEx hhkLowLevelKybd
hhkLowLevelKybd = 0
End If
End Sub
|
|
The keyboard hook looks for the Alt-Tab, Alt-Escape, and Ctrl-Escape key combinations. If it finds one of those, it "eats" the key stroke.
|
|
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_LWIN Or _
p.vkCode = VK_RWIN Or _
p.vkCode = VK_APPS Or _
p.vkCode = VK_CONTROL Or _
p.vkCode = VK_SHIFT Or _
p.vkCode = VK_MENU Or _
((GetKeyState(VK_CONTROL) And &H8000) <> 0) Or _
((p.flags And LLKHF_ALTDOWN) <> 0)
End If
End If
If fEatKeystroke Then
LowLevelKeyboardProc = -1
Else
LowLevelKeyboardProc = CallNextHookEx(0, nCode, _
wParam, ByVal lParam)
End If
End Function
|
|
|
|
|
|