954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Disable Alt+TAB, win key, ctrl esc key

i want to disable the Alt Tab, windows key, ctrl esc key.

FOr my Log On Security that start up in coomputer like windows log on.

Thanks for the help. Try to search hours for this but i cannot find the right solution

im using vb.net 2005 and OS is Vista.

nemzmiyaw
Newbie Poster
15 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 


You can set up a low level hook to do that.

Heres an example that will disable the above.

You may have to go to Project / Properties / Debug and uncheck Enable the Visual Studio Hosting process for it to work while running in Visual Studio.


'Using the Class

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

The Class

Imports System.Runtime.InteropServices
Imports System.Reflection

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 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))

        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
        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
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

wow this work like a charm
Thank You Unhnd_Exception you are great mark this solved.

nemzmiyaw
Newbie Poster
15 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

Exelent!, Works for me with WINDOWS 7 and VB.net 2008

to block Win Key add :

Private Const VK_LWinKey As Integer = 91
Private Const VK_RWinKey As Integer = 92


and

...
ElseIf KeyboardSruct.vkCode = VK_LWinKey Or KeyboardSruct.vkCode = VK_RWinKey Then
   'Left Windows Key o Rigth Windows Key
   Return 1
End If
phyrons
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

Here is my edit on this code. Although the Ctrl Alt Del doesnt get stopped like I thought it would. Using VB 2010 .NET4

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
cj101
Newbie Poster
4 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

Thanks for the code.
Its working good in form load, and form close event.
But in my kiosk application I have to Jam and Unjam multiple times.
When i hit Jam the second time it wont work.
I have tried to modify the code without any luck.
It would be nice if you have a solution to this.
Thank you.

vebe.net
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
Thanks for the code. Its working good in form load, and form close event. But in my kiosk application I have to Jam and Unjam multiple times. When i hit Jam the second time it wont work. I have tried to modify the code without any luck. It would be nice if you have a solution to this. Thank you.

I would also be interested in this because I have the same problem anjust can't figure it out...

I would appreciate any ideas, thx!

OBijac
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: