VBobCat
Well-known member
Dear folks, I need your help on this.
I need to move the mouse pointer to a give x,y coordinate, and then perform a click.
I've found several samples of how to use Windows Api "SendInput" function in order to do so, but they're all in C++/C#, which I can't straightly understand. Nevertheless, I've made a try of adapting those (code below), and it successfully moves the pointer, but doesn't perform the click. Can anyone help me to find the flaw?
Thank you very much!
I need to move the mouse pointer to a give x,y coordinate, and then perform a click.
I've found several samples of how to use Windows Api "SendInput" function in order to do so, but they're all in C++/C#, which I can't straightly understand. Nevertheless, I've made a try of adapting those (code below), and it successfully moves the pointer, but doesn't perform the click. Can anyone help me to find the flaw?
Thank you very much!
Private Sub Test() MouseMove(170,170) Application.DoEvents() MouseLeftClick() End Sub Public Shared Sub MouseMove(ByVal x As Integer, ByVal y As Integer) Dim fx = x * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CXSCREEN) - 1)) Dim fy = y * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CYSCREEN) - 1)) Dim it As New INPUT it.dwType = INPUT_MOUSE it.mkhi.mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE it.mkhi.mi.dx = fx it.mkhi.mi.dy = fy SendInput(1, it, Marshal.SizeOf(it)) End Sub Public Shared Sub MouseLeftClick() Dim it As New INPUT it.dwType = INPUT_MOUSE it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN SendInput(1, it, Marshal.SizeOf(it)) End Sub Private Structure INPUT Dim dwType As Integer Dim mkhi As MOUSEKEYBDHARDWAREINPUT End Structure <StructLayout(LayoutKind.Explicit)> _ Private Structure MOUSEKEYBDHARDWAREINPUT <FieldOffset(0)> Public mi As MOUSEINPUT <FieldOffset(0)> Public ki As KEYBDINPUT <FieldOffset(0)> Public hi As HARDWAREINPUT End Structure Private Structure MOUSEINPUT Public dx As Integer Public dy As Integer Public mouseData As Integer Public dwFlags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure Private Structure KEYBDINPUT Public wVk As Short Public wScan As Short Public dwFlags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure Private Structure HARDWAREINPUT Public uMsg As Integer Public wParamL As Short Public wParamH As Short End Structure Const INPUT_MOUSE As UInt32 = 0 Const INPUT_KEYBOARD As Integer = 1 Const INPUT_HARDWARE As Integer = 2 Const XBUTTON1 As UInt32 = &H1 Const XBUTTON2 As UInt32 = &H2 Const MOUSEEVENTF_MOVE As UInt32 = &H1 Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2 Const MOUSEEVENTF_LEFTUP As UInt32 = &H4 Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8 Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10 Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20 Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40 Const MOUSEEVENTF_XDOWN As UInt32 = &H80 Const MOUSEEVENTF_XUP As UInt32 = &H100 Const MOUSEEVENTF_WHEEL As UInt32 = &H800 Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000 Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000 <DllImport("user32.dll")> Private Shared Function GetSystemMetrics(ByVal smIndex As Integer) As Integer End Function <DllImport("user32.dll")> Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer End Function
Last edited: