2016-03-21 76 views
0

我希望某些代碼在單擊鼠標時運行。我發現的大多數帖子只是在窗體內部或某個對象內單擊鼠標時才執行代碼。我想要代碼在任何地方運行單擊鼠標。這甚至有可能嗎?單擊鼠標時的運行代碼VB

回答

3

您可以使用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