PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Mouse Move and Click with Windows API Function SendInput-VBForums
Results 1 to 5 of 5

Thread: Mouse Move and Click with Windows API Function SendInput

  1. #1

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Mouse Move and Click with Windows API Function SendInput

    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
    Last edited by VBobCat; Sep 13th, 2013 at 10:15 AM. Reason: code lacking
    VBobCat
    VS2013 & Office-VBA
    Autodidact, part-time programmer for job needs

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Mouse Move and Click with Windows API Function SendInput

    Try mouse_event instead. Does it all in one go with far fewer complications.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Re: Mouse Move and Click with Windows API Function SendInput

    Quote Originally Posted by dunfiddlin View Post
    Try mouse_event instead. Does it all in one go with far fewer complications.
    Yes, I'll try and learn to do that, for you've helped me before and I trust your advice. And Pinvoke.net sure is the place to go first. But...

    MSDN states that "This function has been superseded. Use SendInput instead.". What is this but a mislead?

    Besides of all that, why can VB.NET be such a marvelous tool to code fast and straightforward as you learn more and more about it, and at same time be so puny when interacting with its own MS-brand operating system and ActiveX components? Shouldn't it have most of those API functions wrapped in managed classes?

    I say that because it's quite frustrating that a simple "Alert()" javascript that occasionally pops from the document exhibited by WebBrowser control raises no event through which we could properly handle it, and you'll need a timer and some API functions cleverly used (as seen here) simply to close it. It gets worse if you need to answer it "Ok", and worser if the answer needs to be whatever else.

    That said, I thank you very much for the advice. I'll post back my resulting code as soon as I have a result, which I hope is positive.
    VBobCat
    VS2013 & Office-VBA
    Autodidact, part-time programmer for job needs

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Mouse Move and Click with Windows API Function SendInput

    What is this but a mislead?
    Depends how you look at it. Technically it is true that the new method does supersede but at the same time the old one is still available and it works so why not use it? And it does not require any marshalling which makes it the better choice for VB anyway.

    Shouldn't it have most of those API functions wrapped in managed classes?
    Well strictly speaking VB is an abstraction from the Windows API not a complete replacement for it. The API represents the language that the OS speaks across the board whilst VB (and the other .Net languages) are more constrained in intent and operation. Having said that the coverage of API functions is really quite comprehensive. It is only a few slightly unusual circumstances in which you are required to use the API directly, most often for operations that take place outside the application window or when it does not have focus. You can of course move the cursor in managed code ...

    Cursor.Position = New Point(100, 100) (NB. these are screen co-ordinates not form or control co-ordinates)

    ... it's just clicking which requires API.

    I say that because it's quite frustrating that a simple "Alert()" javascript that occasionally pops from the document exhibited by WebBrowser control raises no event through which we could properly handle it
    I understand the frustration but javascript is platform independent and works entirely within the browser environment. It has no direct interaction with the OS from which to raise such an event.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Resolved [SOLVED] Re: Mouse Move and Click with Windows API Function SendInput

    Then, again, it was my fault not to see that everything that goes down must come up, and both keyboard keys and mouse buttons must have a pair of press and release messages in order to simulate the user movement. That is, the correct sub MouseLeftClick stays so:

    vb Code:
    1. Public Shared Sub MouseLeftClick()
    2.         Dim it As New INPUT
    3.         it.dwType = INPUT_MOUSE
    4.         it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
    5.         SendInput(1, it, Marshal.SizeOf(it))
    6.         Application.DoEvents()
    7.         it = New INPUT
    8.         it.dwType = INPUT_MOUSE
    9.         it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTUP
    10.         SendInput(1, it, Marshal.SizeOf(it))
    11. End Sub
    VBobCat
    VS2013 & Office-VBA
    Autodidact, part-time programmer for job needs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width