【问题标题】:Hotkeys not working anymore热键不再起作用
【发布时间】:2016-04-05 16:45:27
【问题描述】:

我一直在使用热键,但不知从何而来,它们不再起作用了。这很混乱。

当我的朋友通过团队查看器检查我的代码时发生了这种情况。然后它停止工作。

Public Class Form2

Public Const MOD_ALT As Integer = &H1 'Alt key
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 Form2_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.E)
    RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.A)
    Me.TopMost = True
    Me.ShowInTaskbar = False
    Me.TransparencyKey = Me.BackColor
    Dim leftpos As Long
    Dim toppos As Long
    leftpos = (My.Computer.Screen.WorkingArea.Right - 2) - Me.Width
    toppos = (My.Computer.Screen.WorkingArea.Bottom - 2) - Me.Height
    Me.Location = New Point(leftpos, toppos)
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_HOTKEY Then
        Dim id As IntPtr = m.WParam
        Select Case (id.ToString)
            Case "100"
                Application.Exit()
            Case "200"
                Form3.Show()

        End Select
    End If
    MyBase.WndProc(m)
End Sub

Private Sub Form2_FormClosing(ByVal sender As System.Object, _
                    ByVal e As System.Windows.Forms.FormClosingEventArgs) _
                    Handles MyBase.FormClosing
    UnregisterHotKey(Me.Handle, 100)
    UnregisterHotKey(Me.Handle, 200)
End Sub


Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Enter Then

        Form3.Show()
        Form3.Activate()

    End If
End Sub
End Class

【问题讨论】:

  • 执行过程中有没有报错信息?
  • 没有,没有错误
  • 就好像没有注册一样

标签: vb.net hotkeys


【解决方案1】:

我在 Dreamincode 中的好人 IronRazer 的帮助下修复了一些 api 函数签名,如下所示

 <DllImport("user32.dll", EntryPoint:="RegisterHotKey")> _
Private Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll", EntryPoint:="UnregisterHotKey")> _
Private Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

  and it worked fine.

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2016-05-23
    • 2019-04-29
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多