Results 1 to 5 of 5

Thread: **RESOLVED** [2005] SendInput API

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    **RESOLVED** [2005] SendInput API

    Hello :-)

    Im trying to click the mouse using the SendInput API, according to MSDN, this is the best API to use because the mouse_event API is depreciated.

    I managed to get it to run without errors but it does not click the mouse :-(

    So could some of you friendly people please check out my code at: http://pastebin.com/655746 and try to find the soloution to the problem that I am having. You need one button on your form to make my example run properly.
    It has been bugging for a few days now and this is the furthest i have got with the API.

    P.S: Im using Visual Studio 2005 Professional Edition with VB.NET.

    Thanks so much,
    Richard Ward
    Last edited by Rich43; Apr 12th, 2006 at 02:40 PM.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: [2005] SendInput API

    Hmm needed a bit of tidying up, the constants had no type and so were objects. Here's what I changed:

    VB Code:
    1. <DllImport("user32.dll", SetLastError:=True)> _
    2.     Private Shared Function SendInput( _
    3.                 ByVal cInputs As Integer, _
    4.                 ByRef pInputs As INPUT, _
    5.                 ByVal cbSize As Integer) As Integer
    6.     End Function
    7.  
    8.     Const INPUT_MOUSE = 0
    9.  
    10.     ' Flattened - nested structures are problematic...
    11.     Private Structure INPUT
    12.         Public dwType As Integer            ' starts at 0  
    13.         Public Mousedx As Integer           ' 4
    14.         Public Mousedy As Integer           ' 8
    15.         Public MousemouseData As Integer    ' 12
    16.         Public MousedwFlags As Integer      ' 16
    17.         Public Mousetime As Integer         ' 20
    18.         Public MousedwExtraInfo As IntPtr   ' 24 - 28
    19.     End Structure
    20.  
    21.     ' These had no type and so were objects!!
    22.     Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    23.     Const MOUSEEVENTF_LEFTUP As Integer = &H4
    24.     Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
    25.     Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
    26.     Const MOUSEEVENTF_MOVE As Integer = &H1
    27.     Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
    28.     Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
    29.     Const MOUSEEVENTF_RIGHTUP As Integer = &H10
    30.  
    31.  
    32.     Sub ClickMouse(ByVal MouseButton As Integer)
    33.         Dim inputevents As New INPUT            
    34.         inputevents.Mousedx = 0
    35.         inputevents.Mousedy = 0
    36.         inputevents.MousemouseData = 0
    37.         inputevents.MousedwFlags = MouseButton
    38.         inputevents.Mousetime = 0
    39.         inputevents.dwType = INPUT_MOUSE              
    40.         Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT))
    41.         Dim result As Integer = SendInput(1, inputevents, cbSize)
    42.         Me.Text = "result: " & result
    43.         Dim lasterror As Integer = Marshal.GetLastWin32Error
    44.         Debug.WriteLine("LastError: " & lasterror)
    45.     End Sub
    46.  
    47.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    48.         'move mouse to the start button
    49.         Dim newMousePosition As New System.Drawing.Point
    50.         newMousePosition.X = 20
    51.         newMousePosition.Y = Screen.PrimaryScreen.Bounds.Height - 20
    52.         System.Windows.Forms.Cursor.Position = newMousePosition
    53.         Windows.Forms.Application.DoEvents()
    54.         ClickMouse(MOUSEEVENTF_LEFTDOWN) 'press left button
    55.         ClickMouse(MOUSEEVENTF_LEFTUP) 'release left button
    56.     End Sub

    This link (c#, but it all applies) is on using structs with api calls:
    http://www.vsj.co.uk/articles/display.asp?id=501
    Last edited by jo0ls; Apr 12th, 2006 at 02:33 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Re: [2005] SendInput API

    Thank you! Il try it now!

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: [2005] SendInput API

    was just editing, I made it more complicated than required...

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Re: [2005] SendInput API

    oh my god, your like some kind of VB.NET god!!!!

    You have any idea how much hard work ive been through with that and you do it in like 5-10 minutes! damn im jelous :-(

Posting Permissions

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



Click Here to Expand Forum to Full Width