Registering 3 hotkeys? Possible?

≡放荡痞女 提交于 2019-12-28 06:26:34

问题


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:


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


来源:https://stackoverflow.com/questions/14168258/registering-3-hotkeys-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!