积极答复者
vb.net 2010 做全局键盘钩子的实例...

问题
答案
-
你说的这种情况可以不做钩子,注册个热键就好了, 我把我做的sample传到这了:http://sdrv.ms/OfhMGY
其中第一个project里的SetHotKeys form就是我说的form,你可以看看里面的代码。
算了,我这边也贴一份吧:
Imports System.Runtime.InteropServices Public Class SetHotKeys <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 Public Shared HotKey(0 To 7) As String Public Const WM_HOTKEY As Integer = &H312 Private Sub SetHotKeys_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing UnregisterHotKey(Me.Handle, 100) UnregisterHotKey(Me.Handle, 200) UnregisterHotKey(Me.Handle, 300) UnregisterHotKey(Me.Handle, 400) End Sub Private Sub SetHotKeys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load HotKey(0) = Asc("Z") 'Z HotKey(1) = Asc("Y") 'Y HotKey(2) = Asc("X") 'X RegisterHotKey(Me.Handle, 100, 0, HotKey(0)) RegisterHotKey(Me.Handle, 200, 0, HotKey(1)) RegisterHotKey(Me.Handle, 300, 0, HotKey(2)) 'this work RegisterHotKey(Me.Handle, 400, 0, Keys.A) 'this also works 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" Console.WriteLine("100") Case "200" Console.WriteLine("200") Case "300" Console.WriteLine("300") Case "400" Me.WindowState = FormWindowState.Normal Console.WriteLine("400") End Select End If MyBase.WndProc(m) End Sub End Class
希望有用。
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Loenly_wind 2012年9月18日 0:54
全部回复
-
你说的这种情况可以不做钩子,注册个热键就好了, 我把我做的sample传到这了:http://sdrv.ms/OfhMGY
其中第一个project里的SetHotKeys form就是我说的form,你可以看看里面的代码。
算了,我这边也贴一份吧:
Imports System.Runtime.InteropServices Public Class SetHotKeys <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 Public Shared HotKey(0 To 7) As String Public Const WM_HOTKEY As Integer = &H312 Private Sub SetHotKeys_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing UnregisterHotKey(Me.Handle, 100) UnregisterHotKey(Me.Handle, 200) UnregisterHotKey(Me.Handle, 300) UnregisterHotKey(Me.Handle, 400) End Sub Private Sub SetHotKeys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load HotKey(0) = Asc("Z") 'Z HotKey(1) = Asc("Y") 'Y HotKey(2) = Asc("X") 'X RegisterHotKey(Me.Handle, 100, 0, HotKey(0)) RegisterHotKey(Me.Handle, 200, 0, HotKey(1)) RegisterHotKey(Me.Handle, 300, 0, HotKey(2)) 'this work RegisterHotKey(Me.Handle, 400, 0, Keys.A) 'this also works 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" Console.WriteLine("100") Case "200" Console.WriteLine("200") Case "300" Console.WriteLine("300") Case "400" Me.WindowState = FormWindowState.Normal Console.WriteLine("400") End Select End If MyBase.WndProc(m) End Sub End Class
希望有用。
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Loenly_wind 2012年9月18日 0:54