0

I am making a program in which I need to set multiple hotkeys that can be pressed by the user. I declared the hotkeys like this:

Dim index1 As Integer = 0
    Dim hotkeycounter as integer= 0
    For index0 = 0 To mainArr.GetUpperBound(0)
        For index1 = 0 To mainArr.GetUpperBound(1)
            If mainArr(index0, index1) = "" Then
                MsgBox("exited the unregisterhotkey for loop")
                GoTo line1
            End If
            UnregisterHotKey(Me.Handle, hotkeycounter)
            hotkeycounter = hotkeycounter + 1
        Next
    Next
line1:

    hotkeycounter = 0
    For index0 = 0 To mainArr.GetUpperBound(0)
        For index1 = 0 To index1 = 1
            If mainArr(index0, index1) = "" Then
                MsgBox("exited the registerhotkey for loop")
                GoTo line2
            End If
            RegisterHotKey(Me.Handle, hotkeycounter, &H1, index0)
            hotkeycounter = hotkeycounter + 1
            MsgBox("A hotkey has been registered")
            MsgBox(($"{mainArr(index0, index1)} "))
        Next
    Next
line2:

So basically the command values for the hotkey are read out of an array. My issue is that I am unable to detect which array is pressed (defined by 'hotkeycounter'). My code to try to detect which hotkey is pressed is:

 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Dim specialcharacter As String
    If m.Msg = WM_HOTKEY Then   

        If commandID = 0 Then
            MsgBox("You pressed the first key combination")
        ElseIf commandID = 1 Then
            MsgBox("You pressed the second key combination")
        ElseIf commandID = 2 Then
            MsgBox("you pressed the third key combination")
        End If
    End If
        MyBase.WndProc(m)
End Sub
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • You have to use the `WParam` and `LParam` of the `Message` to determine which key and modifiers were pressed. See [this thread](https://stackoverflow.com/questions/14168258/registering-3-hotkeys-possible). – jmcilhinney Jun 23 '18 at 04:35
  • Hm, but when I declare the hotkey normally [as in by doing RegisterHotKey(Me.Handle, hotkeycounter, &H1, 65)], I can detect it. Can you see anything wrong with the first part (the one where I am registering the hotkeys), as I am pretty sure that the issue must be in that part. The array is declared as [Public mainArr(99, 1) As String], where the command value is stored in the first box in it's ascii character code form. Thanks for taking the time to respond. – Dario Killen Jun 23 '18 at 05:56
  • Did you read the documentation for the `RegisterHotKey` function? This is the part that should be of interest to you right now: "Return value Type: BOOL If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.". The function will tell you if it succeeds or fails and you are ignoring that information. – jmcilhinney Jun 23 '18 at 06:56
  • Okay, so MsgBox(Err.LastDllError) returns 0, which based on what I read in the registerhotkey documentation means that it failed. Can you not have a variable in the vk [in] field of the registerhotkey function? – Dario Killen Jun 23 '18 at 09:31
  • That is not the correct way to check for errors. The pinvoke declaration is not correct either. And the message handling is wrong. Multiple mistakes here, just look at code that gets this right. Like the code in [this post](https://stackoverflow.com/a/23721587/17034). – Hans Passant Jun 23 '18 at 10:59

0 Answers0