By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

restrict alt+tab and start menu from keyboard while program executing

P: 27
How can I restrict alt+tab and start menu from keyboard while program executing(VB)?I am posting what I tried--

form.frm

Option Explicit
Private Sub Form_Load()
HookKeyboard
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnhookKeyboard
End Sub


module1.bas

Option Explicit

Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type

' Low-Level Keyboard Constants
Public Const HC_ACTION = 0
Public Const LLKHF_EXTENDED = &H1
Public Const LLKHF_INJECTED = &H10
Public Const LLKHF_ALTDOWN = &H20
Public Const LLKHF_UP = &H80

' Virtual Keys
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_DELETE = &H2E

Public Const WH_KEYBOARD_LL = 13&
Public KeyboardHandle As Long

Public KeyboardHook As KeyboardHook

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 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 Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Sub HookedState(ByVal Hooked As Boolean, _
ByVal Text As String)
If (Hooked) Then Debug.Print Text
End Sub
' Implement this function to block as many key combinations as
' you'd like
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) _
As Boolean
If (KeyboardHook Is Nothing) Then
IsHooked = False
Exit Function
End If

' Checks for Ctrl + Esc
If (Hookstruct.vkCode = VK_ESCAPE) And _
CBool(GetAsyncKeyState(VK_CONTROL) _
And &H8000) Then

IsHooked = KeyboardHook.BlockControlEscape

Call HookedState(IsHooked, "Ctrl + Esc blocked")
Exit Function
End If

' Checks for Alt + Tab
If (Hookstruct.vkCode = VK_TAB) And _
CBool(Hookstruct.flags And _
LLKHF_ALTDOWN) Then

IsHooked = KeyboardHook.BlockAltTab

Call HookedState(IsHooked, "Alt + Tab blockd")
Exit Function
End If

' Checks for Alt + Esc
If (Hookstruct.vkCode = VK_ESCAPE) And _
CBool(Hookstruct.flags And _
LLKHF_ALTDOWN) Then

IsHooked = KeyboardHook.BlockAltEscape

Call HookedState(IsHooked, "Alt + Escape blocked")
Exit Function
End If

End Function
Public Function KeyboardCallback(ByVal Code As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Static Hookstruct As KBDLLHOOKSTRUCT

If (Code = HC_ACTION) Then
' Copy the keyboard data out of the lParam (which is a pointer)
Call CopyMemory(Hookstruct, ByVal lParam, Len(Hookstruct))

If (IsHooked(Hookstruct)) Then
KeyboardCallback = 1
Exit Function
End If

End If

KeyboardCallback = CallNextHookEx(KeyboardHandle, _
Code, wParam, lParam)

End Function
Public Sub HookKeyboard()
KeyboardHandle = SetWindowsHookEx( _
WH_KEYBOARD_LL, AddressOf KeyboardCallback, _
App.hInstance, 0&)

Call CheckHooked
End Sub
Public Sub CheckHooked()
If (Hooked) Then
Debug.Print "Keyboard hooked"
Else
Debug.Print "Keyboard hook failed: " & Err.LastDllError
End If
End Sub
Public Function Hooked()
Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
If (Hooked) Then
Call UnhookWindowsHookEx(KeyboardHandle)
End If
End Sub

class.cls

Public Function BlockAltEscape() As Boolean
BlockAltEscape = True
End Function
Public Function BlockAltTab() As Boolean
BlockAltTab = True
End Function
Public Function BlockControlEscape() As Boolean
BlockControlEscape = True
End Function

(I got it from net,but its not working.Any new idea?)
Thaking you!
Jun 11 '08 #1
Share this Question
Share on Google+
2 Replies


debasisdas
Expert 5K+
P: 8,127
You can't handle the Window key in VB.
Jun 11 '08 #2

lotus18
100+
P: 866
Why do you need to restrict these keys? For what purpose? What kind of application are you developing?

Just look for the keyascii codes of these keys and if they hit these keys show a messagebox. be sure that your form is set to keypreview=true.

Rey Sean
Jun 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.