星期五, 11月 05, 2010

利用 vb.net 偵測全域的滑鼠及鍵盤動作

使用windowshooklib

http://www.vbforums.com/showthread.php?t=436321


http://www.code2point.com/Project.aspx?proj=4

使用方法就是下載其 WindowsHookLib.dll , 在 .net 專案中加入此參考,





Imports WindowsHook

Public Class Form1
Dim WithEvents mHook As New MouseHook

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Install the mouse hook
mHook.InstallHook()
Catch ex As Exception
MessageBox.Show("Failed to install the mouse hook!." _
& Environment.NewLine & ex.Message, "Hook Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'Remove the mouse hook
mHook.RemoveHook()
Catch ex As Exception
MessageBox.Show("Error removing the mouse hook!." _
& Environment.NewLine & ex.Message, "Hook Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub mHook_MouseDown(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles mHook.MouseDown
'Set the Handled property for the mouse down event
'to block the mouse down message for the
'controls that are not in the alowedList.
'e.Handled = Not Me.alowedList.Contains(CType(sender, IntPtr))
'Do some other things here
'...
'Me.TextBox1.Text = "mouse down"
Me.TextBox1.Text = e.Button.ToString
'...
End Sub
End Class


沒有留言: