Quantcast
Channel: VBForums - API
Viewing all articles
Browse latest Browse all 168

Mouse Move and Click with Windows API Function SendInput

$
0
0
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!


vb Code:
  1. Private Sub Test()
  2.         MouseMove(170, 170)
  3.         Application.DoEvents()
  4.         MouseLeftClick()
  5.     End Sub
  6.  
  7.     Public Shared Sub MouseMove(ByVal x As Integer, ByVal y As Integer)
  8.         Dim fx = x * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CXSCREEN) - 1))
  9.         Dim fy = y * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CYSCREEN) - 1))
  10.         Dim it As New INPUT
  11.         it.dwType = INPUT_MOUSE
  12.         it.mkhi.mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE
  13.         it.mkhi.mi.dx = fx
  14.         it.mkhi.mi.dy = fy
  15.         SendInput(1, it, Marshal.SizeOf(it))
  16.     End Sub
  17.  
  18.     Public Shared Sub MouseLeftClick()
  19.         Dim it As New INPUT
  20.         it.dwType = INPUT_MOUSE
  21.         it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
  22.         SendInput(1, it, Marshal.SizeOf(it))
  23.     End Sub
  24.  
  25.     Private Structure INPUT
  26.         Dim dwType As Integer
  27.         Dim mkhi As MOUSEKEYBDHARDWAREINPUT
  28.     End Structure
  29.  
  30.     <StructLayout(LayoutKind.Explicit)> _
  31.     Private Structure MOUSEKEYBDHARDWAREINPUT
  32.         <FieldOffset(0)> Public mi As MOUSEINPUT
  33.         <FieldOffset(0)> Public ki As KEYBDINPUT
  34.         <FieldOffset(0)> Public hi As HARDWAREINPUT
  35.     End Structure
  36.  
  37.     Private Structure MOUSEINPUT
  38.         Public dx As Integer
  39.         Public dy As Integer
  40.         Public mouseData As Integer
  41.         Public dwFlags As Integer
  42.         Public time As Integer
  43.         Public dwExtraInfo As Integer
  44.     End Structure
  45.  
  46.     Private Structure KEYBDINPUT
  47.         Public wVk As Short
  48.         Public wScan As Short
  49.         Public dwFlags As Integer
  50.         Public time As Integer
  51.         Public dwExtraInfo As Integer
  52.     End Structure
  53.  
  54.     Private Structure HARDWAREINPUT
  55.         Public uMsg As Integer
  56.         Public wParamL As Short
  57.         Public wParamH As Short
  58.     End Structure
  59.  
  60.     Const INPUT_MOUSE As UInt32 = 0
  61.     Const INPUT_KEYBOARD As Integer = 1
  62.     Const INPUT_HARDWARE As Integer = 2
  63.     Const XBUTTON1 As UInt32 = &H1
  64.     Const XBUTTON2 As UInt32 = &H2
  65.     Const MOUSEEVENTF_MOVE As UInt32 = &H1
  66.     Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2
  67.     Const MOUSEEVENTF_LEFTUP As UInt32 = &H4
  68.     Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8
  69.     Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10
  70.     Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20
  71.     Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40
  72.     Const MOUSEEVENTF_XDOWN As UInt32 = &H80
  73.     Const MOUSEEVENTF_XUP As UInt32 = &H100
  74.     Const MOUSEEVENTF_WHEEL As UInt32 = &H800
  75.     Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000
  76.     Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000
  77.  
  78.     <DllImport("user32.dll")> Private Shared Function GetSystemMetrics(ByVal smIndex As Integer) As Integer
  79.     End Function
  80.     <DllImport("user32.dll")> Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
  81.     End Function
  82.  
  83.     Public Enum SystemMetric As Integer
  84.         ''' <summary>
  85.         '''  Width of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor HORZRES).
  86.         ''' </summary>
  87.         SM_CXSCREEN = 0
  88.         ''' <summary>
  89.         ''' Height of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor VERTRES).
  90.         ''' </summary>
  91.         SM_CYSCREEN = 1
  92.     End Enum

Viewing all articles
Browse latest Browse all 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>