none
VB2008 注册全局热键 RRS feed

答案

  • 你好,

    可以使用RegisterHotKey通过PInvoke去实现你的需要,下面是一个VB.NET的例子:

    Public Class Form5
        Const MYKEYID1 As Integer = 9
        Const MYKEYID2 As Integer = 10
        Public Const MOD_ALT As Integer = &H1 'Alt key
        Public Const VK_NUMPAD1 As Integer = &H61 'NumPad 1 key
        Public Const VK_NUMPAD2 As Integer = &H62 'NumPad 2 key
        Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
        Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        Public Const WM_HOTKEY As Integer = &H312
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID1 Then
                Me.Text = DateTime.Now.Second.ToString & "key 1"
            ElseIf m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID2 Then
                Me.Text = DateTime.Now.Second.ToString & "key 2"
            End If
            MyBase.WndProc(m) 'Never Forget This
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            UnregisterHotKey(Me.Handle, MYKEYID1) 'Remember to unregister the hotkey
            UnregisterHotKey(Me.Handle, MYKEYID2) 'Remember to unregister the hotkey
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RegisterHotKey(Me.Handle, MYKEYID1, MOD_ALT, VK_NUMPAD1) 'Registers Alt + Numpad 1
            RegisterHotKey(Me.Handle, MYKEYID2, MOD_ALT, VK_NUMPAD2) 'Registers Alt + Numpad 2
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年6月16日 2:32
    版主

全部回复

  • 你好 你可以参考下 http://www.tansea.cn/article.asp?id=114
    Denn ich gehor nur dir!坚持不懈!http://hi.baidu.com/1987raymond
    2009年6月13日 10:30
    版主
  • ⊙﹏⊙b汗……
    是VB2008,不是C
    2009年6月13日 10:50
  • 你好,

    可以使用RegisterHotKey通过PInvoke去实现你的需要,下面是一个VB.NET的例子:

    Public Class Form5
        Const MYKEYID1 As Integer = 9
        Const MYKEYID2 As Integer = 10
        Public Const MOD_ALT As Integer = &H1 'Alt key
        Public Const VK_NUMPAD1 As Integer = &H61 'NumPad 1 key
        Public Const VK_NUMPAD2 As Integer = &H62 'NumPad 2 key
        Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
        Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        Public Const WM_HOTKEY As Integer = &H312
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID1 Then
                Me.Text = DateTime.Now.Second.ToString & "key 1"
            ElseIf m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID2 Then
                Me.Text = DateTime.Now.Second.ToString & "key 2"
            End If
            MyBase.WndProc(m) 'Never Forget This
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            UnregisterHotKey(Me.Handle, MYKEYID1) 'Remember to unregister the hotkey
            UnregisterHotKey(Me.Handle, MYKEYID2) 'Remember to unregister the hotkey
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RegisterHotKey(Me.Handle, MYKEYID1, MOD_ALT, VK_NUMPAD1) 'Registers Alt + Numpad 1
            RegisterHotKey(Me.Handle, MYKEYID2, MOD_ALT, VK_NUMPAD2) 'Registers Alt + Numpad 2
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年6月16日 2:32
    版主