locked
disable alt+tab,windows key RRS feed

  • Question

  • hello all,
    how can i disable alt+tab,ctrl+esc,windows key,etc with vb.net??

    i searched from google i got this code

    Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer 
        Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As Integer) As Integer 
        Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer 
        Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer 
     
        Const WH_KEYBOARD_LL = 13 
     
        Structure KBDLLHOOKSTRUCT 
            Dim vkCode As Integer 
            Dim scanCode As Integer 
            Dim flags As Integer 
            Dim time As Integer 
            Dim dwExtraInfo As Integer 
        End Structure 
     
        Dim intLLKey As Integer 
     
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32(), 0) 
        End Sub 
     
        Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 
            UnhookWindowsHookEx(intLLKey) 
        End Sub 
     
        Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer 
            Dim blnEat As Boolean = False 
     
            Select Case wParam 
                Case 256, 257, 260, 261 
                    'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key 
                    blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or _ 
                    ((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or _ 
                    ((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or _ 
                    ((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or _ 
                    ((lParam.vkCode = 92) AndAlso (lParam.flags = 1)) 
            End Select 
     
            If blnEat = True Then 
                Return 1 
            Else 
                Return CallNextHookEx(0, nCode, wParam, lParam) 
            End If 
        End Function 

    but when i used the code and executed my .exe file and press alt+tab,windows key,etc... the error message appeared.
    it said "attempted to read write protected memory. this is often an indication that other memory is corrupt"..
    can someone tell me what the problem is??
    there is other ways to disable alt+tab,ctrl+esc,windows key,etc with vb.net??

    Thx
    Sunday, March 15, 2009 2:39 PM

Answers

  • I made several changes to your code. Note: some data types shall be IntPtr instead of integer. And the call to SetWindowsHookEx has some parameters changed.

    It works on my side now:

     
    1     Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As IntegerByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As IntegerAs IntPtr  
    2     Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Boolean 
    3     Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As IntPtr, ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    4     Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    5  
    6     Const WH_KEYBOARD_LL As Integer = 13  
    7  
    8     Structure KBDLLHOOKSTRUCT  
    9         Dim vkCode As Integer 
    10         Dim scanCode As Integer 
    11         Dim flags As Integer 
    12         Dim time As Integer 
    13         Dim dwExtraInfo As Integer 
    14     End Structure 
    15  
    16     Dim intLLKey As IntPtr  
    17  
    18     Private Sub frmMain_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
    19         intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, IntPtr.Zero, 0)  
    20     End Sub 
    21  
    22     Private Sub frmMain_Closing(ByVal sender As ObjectByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing  
    23         UnhookWindowsHookEx(intLLKey)  
    24     End Sub 
    25  
    26     Private Function LowLevelKeyboardProc(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    27         Dim blnEat As Boolean = False 
    28  
    29         Select Case wParam  
    30             Case 256, 257, 260, 261  
    31                 'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key    
    32                 blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or _  
    33                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or _  
    34                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or _  
    35                 ((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or _  
    36                 ((lParam.vkCode = 92) AndAlso (lParam.flags = 1))  
    37         End Select 
    38  
    39         If blnEat = True Then 
    40             Return 1  
    41         Else 
    42             Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)  
    43         End If 
    44     End Function 
    45  

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 17, 2009 11:30 AM

All replies

  • Many times these requests come because people are building a kiosk type application and you don't want users moving away from the application.  If that is the case, there are things built into windows that allow you to lock things down.  Group Policy will help you disable most, if not all of those keys, and no programming is required.
    Negative0 http://johnkoerner.com
    Monday, March 16, 2009 12:12 AM
  •  I think Negative0 is right, group policy can help you disable without doing programming. Most of the settings are saved in the registry. So you can search for the location and can make the changes there also (but with caution).

    You can read/write to registry using methods My.Computer.Registry class
    Gaurav Khanna
    Monday, March 16, 2009 4:22 AM
  • I made several changes to your code. Note: some data types shall be IntPtr instead of integer. And the call to SetWindowsHookEx has some parameters changed.

    It works on my side now:

     
    1     Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As IntegerByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As IntegerAs IntPtr  
    2     Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Boolean 
    3     Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As IntPtr, ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    4     Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    5  
    6     Const WH_KEYBOARD_LL As Integer = 13  
    7  
    8     Structure KBDLLHOOKSTRUCT  
    9         Dim vkCode As Integer 
    10         Dim scanCode As Integer 
    11         Dim flags As Integer 
    12         Dim time As Integer 
    13         Dim dwExtraInfo As Integer 
    14     End Structure 
    15  
    16     Dim intLLKey As IntPtr  
    17  
    18     Private Sub frmMain_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
    19         intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, IntPtr.Zero, 0)  
    20     End Sub 
    21  
    22     Private Sub frmMain_Closing(ByVal sender As ObjectByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing  
    23         UnhookWindowsHookEx(intLLKey)  
    24     End Sub 
    25  
    26     Private Function LowLevelKeyboardProc(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    27         Dim blnEat As Boolean = False 
    28  
    29         Select Case wParam  
    30             Case 256, 257, 260, 261  
    31                 'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key    
    32                 blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or _  
    33                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or _  
    34                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or _  
    35                 ((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or _  
    36                 ((lParam.vkCode = 92) AndAlso (lParam.flags = 1))  
    37         End Select 
    38  
    39         If blnEat = True Then 
    40             Return 1  
    41         Else 
    42             Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)  
    43         End If 
    44     End Function 
    45  

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 17, 2009 11:30 AM
  • thx wang,jie
    you saved me :)
    Wednesday, March 18, 2009 6:45 AM
  • But Wang, your code only work on the higher version of Windows XP, like Vista or 7.

    I can't make your code work on XP.

    But I found this piece of code that can work in XP, but the problem is: it will not work in Vista or 7.

     

    Imports System.Runtime.InteropServices
    Imports System.Reflection
    
    Public Class Form1
     Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
     KeyboardJammer.Jam()
     End Sub
    
     Private Sub Form1_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleDestroyed
     KeyboardJammer.UnJam()
     End Sub
    End Class
    
    Public Class KeyboardJammer
    
     Private Delegate Function HookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
     Private Shared HookDelegate As HookCallback
     Private Shared HookId As Integer
     Private Const Wh_Keyboard_LL As Integer = 13
     Private Const Vk_Tab As Integer = 9
     Private Const Vk_Escape As Integer = 27
     Private Const vk_Del As Integer = 46
     Private Const Vk_F4 As Integer = 115
     Private Const VK_LWinKey As Integer = 91
     Private Const VK_RWinKey As Integer = 92
    
     Private Shared Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
     'All keyboard events will be sent here.
    
     'Don't process just pass along.
     If nCode < 0 Then
      Return CallNextHookEx(HookId, nCode, wParam, lParam)
     End If
    
     'Extract the keyboard structure from the lparam
     'This will contain the virtual key and any flags.
     'This is using the my.computer.keyboard to get the
     'flags instead
     Dim KeyboardSruct As KBDLLHOOKSTRUCT = Marshal.PtrToStructure(lParam, GetType(KBDLLHOOKSTRUCT))
     MsgBox(KeyboardSruct.vkCode.ToString, MsgBoxStyle.OkOnly)
     If KeyboardSruct.vkCode = Vk_Tab And My.Computer.Keyboard.AltKeyDown Then
      'Alt Tab
      Return 1
     ElseIf KeyboardSruct.vkCode = Vk_Escape And My.Computer.Keyboard.CtrlKeyDown Then
      'Control Escape
      Return 1
     ElseIf KeyboardSruct.vkCode = VK_LWinKey Or KeyboardSruct.vkCode = VK_RWinKey Then
      'Left Windows Key or Rigth Windows Key
      Return 1
     ElseIf KeyboardSruct.vkCode = vk_Del And My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown Then
      'Ctrl Alt Del
      Return 1
     ElseIf KeyboardSruct.vkCode = Vk_F4 And My.Computer.Keyboard.AltKeyDown Then
      'Alt F4
      Return 1
     End If
    
     'Send the message along 
     Return CallNextHookEx(HookId, nCode, wParam, lParam)
    
     End Function
    
     Public Shared Sub Jam()
     'Add the low level keyboard hook
     If HookId = 0 Then
      HookDelegate = AddressOf KeyBoardHookProc
      HookId = SetWindowsHookEx(Wh_Keyboard_LL, HookDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
    
      If HookId = 0 Then
      'error
      End If
    
     End If
     End Sub
    
     Public Shared Sub UnJam()
     'Remove the hook
     UnhookWindowsHookEx(HookId)
     End Sub
    
     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
     Private Shared Function CallNextHookEx( _
     ByVal idHook As Integer, _
     ByVal nCode As Integer, _
     ByVal wParam As IntPtr, _
     ByVal lParam As IntPtr) As Integer
     End Function
    
     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
     Private Shared Function SetWindowsHookEx( _
      ByVal idHook As Integer, _
      ByVal HookProc As HookCallback, _
      ByVal hInstance As IntPtr, _
      ByVal wParam As Integer) As Integer
     End Function
    
     <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
     Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
     End Function
    
     Private Structure KBDLLHOOKSTRUCT
     Public vkCode As Integer
     Public scanCode As Integer
     Public flags As Integer
     Public time As Integer
     Public dwExtraInfo As IntPtr
     End Structure
    
    End Class
    

     

    I don't understand why the keyboard hook won't work in both platform.

    Please explain to my why.. Thx..



    Thursday, May 26, 2011 1:45 AM
  • I made several changes to your code. Note: some data types shall be IntPtr instead of integer. And the call to SetWindowsHookEx has some parameters changed.

    It works on my side now:

     
    1     Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As IntegerByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As IntegerAs IntPtr  
    2     Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Boolean 
    3     Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As IntPtr, ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    4     Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    5  
    6     Const WH_KEYBOARD_LL As Integer = 13  
    7  
    8     Structure KBDLLHOOKSTRUCT  
    9         Dim vkCode As Integer 
    10         Dim scanCode As Integer 
    11         Dim flags As Integer 
    12         Dim time As Integer 
    13         Dim dwExtraInfo As Integer 
    14     End Structure 
    15  
    16     Dim intLLKey As IntPtr  
    17  
    18     Private Sub frmMain_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
    19         intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, IntPtr.Zero, 0)  
    20     End Sub 
    21  
    22     Private Sub frmMain_Closing(ByVal sender As ObjectByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing  
    23         UnhookWindowsHookEx(intLLKey)  
    24     End Sub 
    25  
    26     Private Function LowLevelKeyboardProc(ByVal nCode As IntegerByVal wParam As IntegerByRef lParam As KBDLLHOOKSTRUCT) As Integer 
    27         Dim blnEat As Boolean = False 
    28  
    29         Select Case wParam  
    30             Case 256, 257, 260, 261  
    31                 'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key    
    32                 blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or _  
    33                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or _  
    34                 ((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or _  
    35                 ((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or _  
    36                 ((lParam.vkCode = 92) AndAlso (lParam.flags = 1))  
    37         End Select 
    38  
    39         If blnEat = True Then 
    40             Return 1  
    41         Else 
    42             Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)  
    43         End If 
    44     End Function 
    45  

    This posting is provided "AS IS" with no warranties, and confers no rights.

    How about the Ctrl+Alt+Del??
    Monday, September 12, 2011 7:06 AM
  • How about the Ctrl+Alt+Del??


    Tuesday, September 18, 2012 2:38 PM