Okay hello everyone, so I'm using this class to protect app from user using hotkeys while form is running:

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 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 Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        If nCode < 0 Then
            Return CallNextHookEx(HookId, nCode, wParam, lParam)
        End If
        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
        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 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 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

Now I call it on form open and form close:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        KeyboardJammer.UnJam()
    End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KeyboardJammer.Jam()
    End Sub

I have 1 button that shows settings form and closing this form with code:

settings.Show()
Me.Close()

And in "settings" form i have button that opens back Form1:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newform As New Form1()
        newform.Show()
        Close()
    End Sub

Now problem is, when i run my .exe form1 loads up and you cant use hotkeys like ctrl+esc, alt+f4 etc..
But when I close form1 and open settings, I can use hotkeys which is okay, but when i hit again that Button1 on "Settings" form and my Form1 shows again I can still use HOTKEYS which is NOT GOOD... Any ides whats wrong?

Well, in your settings form button1 you do declare a new form. If you want to just get back to form1 just do: form1.show(). You also don't need to close form1 to show form2. You just can hide form1 using: me.hide(). If all this doesn't work you need to call your function again.

I've fixed it problem was with class code i had to fix code :)

This article has been dead for over six months. Start a new discussion instead.