单击鼠标时运行代码 VB



我希望每当单击鼠标时都能运行某些代码。我发现的大多数帖子仅在表单内或某个对象内单击鼠标时执行代码。我希望代码在单击鼠标的任何地方运行。这可能吗?

您可以使用

GetAsyncKeyState api并检查鼠标左右按钮。下面是一个使用计时器轮询的示例

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("user32.dll")> _
    Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
    End Function
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If GetAsyncKeyState(Keys.LButton) <> 0 Then
            Debug.Print("Left button click")
        ElseIf GetAsyncKeyState(Keys.RButton) <> 0 Then
            Debug.Print("Right button click")
        End If
    End Sub
End Class

最新更新