473,287 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
2 2549
debasisdas
8,127 Expert 4TB
You can't handle the Window key in VB.
Jun 11 '08 #2
lotus18
866 512MB
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

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

Similar topics

3
by: Stefan | last post by:
Hy, i have an app and i must disable this combination: ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this: i find something on Internet and i can block ALT+F4 protected override...
1
by: Jigar mehta | last post by:
Hye, How to create systemwide hooks for keyboard for keys like WIN, ALT+TAB, CTRL+ALT+DEL even when the application does not have the focus on it... Thanks,
6
by: Chris Mason | last post by:
I have an application that runs full screen on a system and I would like to be able to (at times) switch another program using an ALT-TAB like interface. However the system only has a touchscreen...
3
by: dencdr | last post by:
Hi, I have an application VB.NET (using Form Win32) with no icon in TaskBar. When a FORM of this application is visible I see this application in the list of "Alt+Tab" choice. How do for...
4
by: Never Best | last post by:
I have an application. I want it so when the user minimizs the window it goes into the system tray, and while in the system tray I don't want it in the "Alt-Tab" list. (When the program starts it...
9
by: Peter Larsen | last post by:
Hi, I have asked this question before, but did not get anything useful from it - so sorry that i am asking again !! How do i prevent an application from appears more than once in the ALT+TAB...
5
by: kh | last post by:
My app filters windows messages because of some custom window functionality I have implemented. Does my window receive a message when the user selects Alt-Tab? If so which message? Many thanks ...
3
by: gusma728 | last post by:
Is there an easy way to remove the icon of a form (in this case a progress bar) from the alt-tab menu? When the progress bar appears in my appIication the default program icon appears in the alt+tab...
10
by: thupham | last post by:
Dear all friend, I want disable Ctl+Alt+Del; Ctrl+Esc; Ctrl+tab, Alt+Tab, Start button, ctrl+Alt+Del, lock all keys on the keyboard. Have you ever do it in C#. Help me. Thanks for all reply.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.