locked
Hotkeys with hook keyboard RRS feed

  • Question

  • Hello, i want to disable keys with hook keyboard but i need a hotkeys work with disable keys

    i saw this work : 

    If GetAsyncKeyState(Keys.ControlKey) AndAlso _ GetAsyncKeyState(Keys.ShiftKey) AndAlso _ (lParam.vkCode = Keys.A) Then MsgBox("COUCOU") End If

    But i want 2 alphabets keys like "A" and "Z" but this doesn't work:

    If (lParam.vkCode = Keys.A) AndAlso _
                   (lParam.vkCode = Keys.Z) Then
                MsgBox("COUCOU")
            End If

    Why?

    Please help me

    Sorry for my english, i'm french

    Sunday, September 7, 2014 7:26 PM

Answers

  • Hi,

     You shouldn`t be posting the whole keyboard hook code on here. I already said, i didn`t because, it can be used maliciously. You should edit your post and remove that code.

     Anyways, you can try this. I placed any code that accesses Form3 in Try/Catch blocks because, if form3 is not opened and the code in the hook tries to access it then it will probably throw an exception.

     If both the A and Z keys are pressed at the same time then it should close Form3. If only one of the keys are pressed at a time then it will stop them from doing anything because of Returning 1.

        Private kA, kZ As Boolean 'add this to your code
    
        Private Function LowLevelKeyboardProc(ByVal nCode As Int32, ByVal wParam As Int32, ByRef lParam As KBDLLHOOKSTRUCT) As Int32
    
            If nCode = HC_ACTION Then
                Dim KeyData As Keys = CType(lParam.vkCode, Keys)
    
                If wParam = WM_KEYDOWN Then
                    If KeyData = Keys.A Then kA = True
                    If KeyData = Keys.Z Then kZ = True
                ElseIf wParam = WM_KEYUP Then
                    If KeyData = Keys.A Then kA = False
                    If KeyData = Keys.Z Then kZ = False
                End If
    
                If kA And kZ Then
                    Try
                        Form3.Close()
                    Catch ex As Exception
                    End Try
                Else
                    Try
                        If Form3.AKey.Checked = True Then Return 1
                        If Form3.ZKey.Checked = True Then Return 1
                    Catch ex As Exception
                    End Try
                End If
    
            End If
    
            Return CallNextHookEx(kHook, nCode, wParam, lParam) 'the 1st parameter (hHook) is suppose to be the hook handle
        End Function

     If you don`t understand the code then you really should read the msdn documents on the functions or methods to get a better grasp on what they do.   8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, September 8, 2014 1:39 PM
    • Marked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    • Unmarked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    • Marked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    Monday, September 8, 2014 1:33 PM

All replies

  • Hi,

     The keyboard callback is not made to detect 2 standard keys being pressed at the same time. You will have to do a little workaround to catch 2 standard keys being pressed at the same time kind of like below. Of coarse you will have to raise whatever event you have or do whatever you need when both of the boolean variables are True. I just made a quick example that works good for me.

     I am not posting all the code because of the malicious things that keyboard hooks can be used for. You should be able to figure it out from this example if you already have the hook code.  8)

        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_KEYUP As Integer = &H101
        Private kA, kZ As Boolean
    
        Private Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As System.IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
            If Code = HC_ACTION Then
                Dim KeyData As Keys = CType(lParam.vkCode, Keys)
    
                If wParam.ToInt32 = WM_KEYDOWN Then
                    If KeyData = Keys.A Then kA = True
                    If KeyData = Keys.Z Then kZ = True
                ElseIf wParam.ToInt32 = WM_KEYUP Then
                    If KeyData = Keys.A Then kA = False
                    If KeyData = Keys.Z Then kZ = False
                End If
    
                If kA And kZ Then
                    RaiseEvent HotKeyPressed(Me, New EventArgs)
                End If
            End If
    
            Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
    
        End Function
    


    If you say it can`t be done then i`ll try it

    Sunday, September 7, 2014 10:07 PM
  • Thanks for your speed !

    But i'm lost (i'm not really good) i understand, but i think it's not coherent with my hook code

    I just want to do : form3.close , when keys A et Z pressed

    Can you tell me where i put the code you gave to me ?


    • Edited by Luigi21 Monday, September 8, 2014 4:39 PM
    Monday, September 8, 2014 12:21 PM
  • Hi,

     You shouldn`t be posting the whole keyboard hook code on here. I already said, i didn`t because, it can be used maliciously. You should edit your post and remove that code.

     Anyways, you can try this. I placed any code that accesses Form3 in Try/Catch blocks because, if form3 is not opened and the code in the hook tries to access it then it will probably throw an exception.

     If both the A and Z keys are pressed at the same time then it should close Form3. If only one of the keys are pressed at a time then it will stop them from doing anything because of Returning 1.

        Private kA, kZ As Boolean 'add this to your code
    
        Private Function LowLevelKeyboardProc(ByVal nCode As Int32, ByVal wParam As Int32, ByRef lParam As KBDLLHOOKSTRUCT) As Int32
    
            If nCode = HC_ACTION Then
                Dim KeyData As Keys = CType(lParam.vkCode, Keys)
    
                If wParam = WM_KEYDOWN Then
                    If KeyData = Keys.A Then kA = True
                    If KeyData = Keys.Z Then kZ = True
                ElseIf wParam = WM_KEYUP Then
                    If KeyData = Keys.A Then kA = False
                    If KeyData = Keys.Z Then kZ = False
                End If
    
                If kA And kZ Then
                    Try
                        Form3.Close()
                    Catch ex As Exception
                    End Try
                Else
                    Try
                        If Form3.AKey.Checked = True Then Return 1
                        If Form3.ZKey.Checked = True Then Return 1
                    Catch ex As Exception
                    End Try
                End If
    
            End If
    
            Return CallNextHookEx(kHook, nCode, wParam, lParam) 'the 1st parameter (hHook) is suppose to be the hook handle
        End Function

     If you don`t understand the code then you really should read the msdn documents on the functions or methods to get a better grasp on what they do.   8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, September 8, 2014 1:39 PM
    • Marked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    • Unmarked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    • Marked as answer by Luigi21 Monday, September 8, 2014 6:13 PM
    Monday, September 8, 2014 1:33 PM
  • Hi,

    Thank you so much! It works perfectly!

    You're so good


    Monday, September 8, 2014 6:13 PM
  • Hi,

    Thank you so much! It works perfectly!

    You're so good


     Your Welcome. Thanks for the compliment.  8)


    If you say it can`t be done then i`ll try it

    Monday, September 8, 2014 6:20 PM
  • Hi,

    I'm back, i just need to disable ctrl alt delete hotkey, but my hook can't disable this or diable all hotkeys. How can i do that please ?


    • Edited by Luigi21 Thursday, September 11, 2014 8:39 PM
    Thursday, September 11, 2014 8:29 PM
  • Hi,

    I'm back, i just need to disable ctrl alt delete hotkey, but my hook can't disable this or diable all hotkeys. How can i do that please ?


    Hi,

     You can`t disable the Ctrl + Alt + Del hotkey with a low level keyboard hook. I don`t even know if you could do that with a high level hook or not which can`t be made in managed VB code. Besides, i would HIGHLY recommend against doing that. That is for accessing the Task Manager and a few other important options. If you disable that then the user can not kill an application that may be freezing up their system or may be doing harmful things.

     Nothing personal but, even if i knew how to do it i would not post it on the internet because, of the malicious things it could be used for.   8)


    If you say it can`t be done then i`ll try it

    Thursday, September 11, 2014 9:20 PM
  • Yes sure, ok thank you :)
    Friday, September 12, 2014 5:06 AM