Mouse command with Windows API SendInput function - why doesn't it work?

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Dear friends,
I would like to command mouse operations with Windows API SendInput function. So I wrote the code below, but it doesn't work. Can anyone point out what I am doing wrong here?
Thank you very much.

        Public Sub MouseMove(ByVal x As Integer, ByVal y As Integer)
            Dim iMove As INPUT
            iMove.type = INPUT_MOUSE
            iMove.u.mi.dwFlags = MOUSEEVENTF_MOVE
            iMove.u.mi.dx = x
            iMove.u.mi.dy = y
            Dim rmove = SendInput(1, {iMove}, Marshal.SizeOf(iMove))
        End Sub
        Public Sub MouseClick()
            Dim iLeftDown, iLeftUp As INPUT
            iLeftDown.type = INPUT_MOUSE
            iLeftDown.u.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
            iLeftUp.type = INPUT_MOUSE
            iLeftUp.u.mi.dwFlags = MOUSEEVENTF_LEFTUP
            Dim rdown = SendInput(1, {iLeftDown}, Marshal.SizeOf(iLeftDown))
            Dim rup = SendInput(1, {iLeftUp}, Marshal.SizeOf(iLeftUp))
        End Sub
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Function SendInput(ByVal cInputs As Integer, ByRef pInputs As INPUT(), ByVal cbSize As Integer) As Integer
        End Function
        Structure INPUT
            Public type As Integer
            Public u As InputUnion
        End Structure
        Private Const KEYEVENTF_KEYUP As Integer = &H2
        Private Const INPUT_MOUSE As Integer = 0
        Private Const INPUT_KEYBOARD As Integer = 1
        Private Const INPUT_HARDWARE As Integer = 2
        <StructLayout(LayoutKind.Explicit)> _
        Structure InputUnion
            <FieldOffset(0)> Public mi As MOUSEINPUT
            <FieldOffset(0)> Public ki As KEYBDINPUT
            <FieldOffset(0)> Public hi As HARDWAREINPUT
        End Structure
        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 IntPtr
        End Structure
        Private Const MOUSEEVENTF_MOVE = &H1
        Private Const MOUSEEVENTF_LEFTDOWN = &H2
        Private Const MOUSEEVENTF_LEFTUP = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN = &H8
        Private Const MOUSEEVENTF_RIGHTUP = &H10
        Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
        Private Const MOUSEEVENTF_MIDDLEUP = &H40
        Private Const MOUSEEVENTF_ABSOLUTE = &H8000
        Structure KEYBDINPUT
            Public wVk As Short
            Public wScan As Short
            Public dwFlags As Integer
            Public time As Integer
            Public dwExtraInfo As IntPtr
        End Structure
        Structure HARDWAREINPUT
            Public uMsg As Integer
            Public wParamL As Short
            Public wParamH As Short
        End Structure
 

Latest posts

Back
Top