2

Hello I am trying to use RegisterHeyKeys in VB.NET however I got it to work with 2 hotkeys I tried just adding in the third and it's giving a too many arguments. This is probably something really simple and I'm also a nub so go easy. lol. Any help would be greatly appreciated.

Here is the code so far:

Public Const MOD_CONTROL As Integer = &H11
Public Const MOD_SHIFT As Integer = &H10
Public Const WM_HOTKEY As Integer = &H312

<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer, ByVal fsModifiers As Integer, _
ByVal vk As Integer) As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                    ByVal id As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL, MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL, MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL, MOD_SHIFT, Keys.D4)
End Sub

1 Answers1

6

The problem as I see it is you have added two modifiers MOD_CONTROL and MOD_SHIFT and seperated them with a comma saying that you have five parameters to the function even though it only takes four. Try Oring together your Modifers like this. You also should verify your modifier keys with the Documentation they appear to not be correct.

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL Or MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL Or MOD_SHIFT, Keys.D4)
End Sub

From the documentation it states(emphasis mine):

fsModifiers [in]
Type: UINT

The keys that must be pressed in combination with the key specified by the uVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values.

   Value                        Meaning

MOD_ALT 0x0001         Either ALT key must be held down.

MOD_CONTROL 0x0002     Either CTRL key must be held down.

MOD_NOREPEAT 0x4000   Changes the hotkey behavior so that the keyboard auto-repeat does not yield multiple hotkey notifications. 
                      Windows Vista and Windows XP/2000:  This flag is not supported.
MOD_SHIFT 0x0004      Either SHIFT key must be held down.

MOD_WIN 0x0008        Either WINDOWS key was held down. These keys are labeled with the Windows logo. Keyboard shortcuts
                      that involve the WINDOWS key are reserved for use by the operating system

Here is a Working example of your program.

Public Const MOD_CONTROL As Integer = &H2
Public Const MOD_SHIFT As Integer = &H4
Public Const WM_HOTKEY As Integer = &H312

<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                                      ByVal id As Integer, ByVal fsModifiers As Integer, _
                                      ByVal vk As Integer) As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                ByVal id As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL Or MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL Or MOD_SHIFT, Keys.D4)
End Sub

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.DefWndProc(m)
    If m.Msg = WM_HOTKEY Then
        Select Case CType(m.WParam, Integer)
            Case 100
                NotifyIcon1.Text = "Hello"
                NotifyIcon1.ShowBalloonTip(2000, "", NotifyIcon1.Text, ToolTipIcon.Info)
            Case 200
                NotifyIcon1.Text = "World"
                NotifyIcon1.ShowBalloonTip(2000, "", NotifyIcon1.Text, ToolTipIcon.Info)
            Case 300
                NotifyIcon1.Visible = False
                If Not Visible Then Visible = True
        End Select
    End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Me.Hide()
    NotifyIcon1.Icon = Me.Icon
    NotifyIcon1.Visible = True
End Sub
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • I'm not sure I know what you mean by the Modifiers. I know what you mean by the wrong value. But what would be the correct value for these? I don't understand. Sorry, noob question. :P –  Jan 05 '13 at 05:21
  • The modifiers are the MOD_CONTROL and MOD_SHIFT. I included the documentation in the answer, think of them as control keys I was using microsofts nomenclature. the Proper values should be: `MOD_CONTROL` = &H2 and `MOD_SHIFT` = &H4 – Mark Hall Jan 05 '13 at 05:26
  • @JJR. I just added the example that I used to test with – Mark Hall Jan 05 '13 at 05:34
  • Ah I see. Well this code works, and thank you very much for it however, it still does not work when it's minimized to tray. I was under the impression from other forum posts and such that if you use Registered Hotkeys it will work even if the program is minimized to the tray. Right now it works in the background when it's not focused however not when it's in the tray. Any ideas? –  Jan 05 '13 at 05:48
  • @JJR. It works when I minimize it normally, how are you minimizing it to tray? – Mark Hall Jan 05 '13 at 05:51
  • Using Me.Hide() when the click a "Minimize to Tray" Button. –  Jan 05 '13 at 05:53
  • @JJR. If you put a breakpoint on the DefWndProc Sub you will find that it still gets activated when you use your hot keys even when the form is hidden. By hiding the form you are making in not visible, it will not show up in the Tray or Taskbar. I just added change to DefWndProc to make form visible when HotKey is pressed – Mark Hall Jan 05 '13 at 05:59
  • I see. So is there no way to get it to recognize the hot keys while in the tray? Part of the neatness of the program is so that it will run in the tray without having to open it at all. Thanks. –  Jan 05 '13 at 06:07
  • @JJR. This question has kind of changed from what you had originaly asked, which I answered. I also asked you how you put your program into the tray and you said Me.Hide which just makes the program not visible, try putting a breakpoint on the DefWndProc and see if it gets hit. Also if you tell me how you are putting it into the Tray I will be more than happy to take a further look at it. I suspect you are using NotifyIcon but I would like to see exactly what you are doing. – Mark Hall Jan 05 '13 at 06:11
  • Yeah sorry about drifting off topic. We can take it to messages if you prefer. Sorry about missing your question. I am indeed using NotifyIcon to run from the tray. –  Jan 05 '13 at 06:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22224/discussion-between-mark-hall-and-jj-r) – Mark Hall Jan 05 '13 at 06:24