vb.net 如何模拟鼠标点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17414301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:06:13  来源:igfitidea点击:

How to simulate mouse click?

vb.netmouseeventmouse

提问by Devonx25

I'm trying to make a program to click with keyboard like in Osu!.

我正在尝试制作一个程序,可以像在Osu一样用键盘点击.

I've tried SendKeys(), RaiseMouseEvent()and OnMouseClick(). Now I'm trying this and can't get anything to work.

我试过了SendKeys()RaiseMouseEvent()而且OnMouseClick()。现在我正在尝试这个并且无法获得任何工作。

The error I keep getting is:

我不断收到的错误是:

PInvoke restriction: cannot return variants.

PInvoke 限制:不能返回变体。

Public Class Form1
    Dim onn As Boolean = False
    Declare Function mc Lib "user32.dll" Alias "mouse_event" (flag, x, y, button, extra)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not onn Then
            Button1.Text = "Off"
            Label1.Text = "Status: On"
            onn = True
        ElseIf onn Then
            Button1.Text = "On"
            Label1.Text = "Status: Off"
            onn = False
        End If
    End Sub
    Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If onn And (e.KeyChar = "Z" Or e.KeyChar = "X" Or e.KeyChar = "z" Or e.KeyChar = "x") Then
            mc(&H2, 0, 0, 0, 0)
            mc(&H4, 0, 0, 0, 0)
        End If
    End Sub
End Class

采纳答案by Idle_Mind

This examples clicks where the mouse currently is when the feature is in the "onn" state:

此示例在功能处于“onn”状态时单击鼠标当前所在的位置:

Public Class Form1

    Private onn As Boolean = False

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
      ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
      ByVal dwExtraInfo As Integer)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        Button1.Text = "Off"
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        onn = Not onn
        Button1.Text = IIf(onn, "On", "Off")
    End Sub

    Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If onn Then
            Select Case e.KeyChar
                Case "Z", "z", "X", "x"
                    mouse_event(&H2, 0, 0, 0, 0)
                    mouse_event(&H4, 0, 0, 0, 0)

            End Select
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Debug.Print("Button2")
    End Sub

    Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        Debug.Print("Button3")
    End Sub

End Class

回答by Juanjo

Public Class Iconform

  Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long

  Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long

  Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

  Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down

  Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

  Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down

  Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up

  Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down

  Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up



  Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.Click

    SettingsForm.Show()



  End Sub



  Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click

    SettingsForm.Show()

  End Sub



  Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click

    Me.Close()



  End Sub

  Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    Dim bHandled As Boolean = False

    Dim xPos As Integer = Windows.Forms.Cursor.Position.X.ToString

    Dim zPos As Integer = Windows.Forms.Cursor.Position.Y.ToString



    Select Case e.KeyCode

      Case Keys.Right

        Windows.Forms.Cursor.Position = New Point(xPos + 10, zPos)

      Case Keys.Left

        Windows.Forms.Cursor.Position = New Point(xPos - 10, zPos)

      Case Keys.Down

        Windows.Forms.Cursor.Position = New Point(xPos, zPos + 10)

      Case Keys.Up

        Windows.Forms.Cursor.Position = New Point(xPos, zPos - 10)

      Case Keys.D

        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)







    End Select

  End Sub

End Class

回答by Edper

Try using the PerformClick()method:

尝试使用以下PerformClick()方法:

Button1.PerformClick()

In your code it could be like:

在您的代码中,它可能类似于:

If onn And (e.KeyChar = "Z" Or e.KeyChar = "X" Or e.KeyChar = "z" Or e.KeyChar = "x") Then

    Button1.PerformClick()
End If