CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: SendInput

  1. #1
    Join Date
    Jan 2003
    Posts
    4

    SendInput

    SendInput is now recommended as opposed to the much simpler mouse_event for XP, etc.

    Most of the code I have seen for it no the web either won't compile or even once compiled doesn't work properly.

    There is basically a routine floating around out there on the web to Press the P key and send a right mouse click. That code sample (commonplace as it is) is riddled with small errors and hence doesn't compile (or work). I have edited it so that is just sends a left mouse-click but it does work and post it below:

    I am a bit nervous about my use of the CopyMemory function however as I don't want to create any memory leakage. If anyone spots a problem with it I would be very grateful if you could point it out!


    Code:
    Option Explicit
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long
    
    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4
    Private Const INPUT_MOUSE = 0
    Private Const INPUT_KEYBOARD = 1
    Private Const INPUT_HARDWARE = 2
    
    Type MOUSEINPUT
      dx As Long
      dy As Long
      mouseData As Long
      dwFlags As Long
      dwtime As Long
      dwExtraInfo As Long
    End Type
    
    Type INPUT_TYPE
      dwType As Long
      xi(0 To 23) As Byte
    End Type
    
    
    Public Sub ClickDaMouse()
    
        Dim intX As Integer
        Dim inputEvents(0 To 1) As INPUT_TYPE ' holds information about each event
        Dim mouseEvent As MOUSEINPUT ' temporarily hold mouse input info
    
        ' Load the information needed to synthesize pressing the left mouse button.
        mouseEvent.dx = 0 ' no horizontal movement
        mouseEvent.dy = 0 ' no vertical movement
        mouseEvent.mouseData = 0 ' not needed
        mouseEvent.dwFlags = MOUSEEVENTF_LEFTDOWN ' left button down
        mouseEvent.dwtime = 0 ' use the default
        mouseEvent.dwExtraInfo = 0 ' not needed
        ' Copy the structure into the input array's buffer.
        inputEvents(0).dwType = INPUT_MOUSE ' mouse input
        CopyMemory inputEvents(0).xi(0), mouseEvent, Len(mouseEvent)
          
        ' Do the same as above, but for releasing the left mouse button.
        mouseEvent.dx = 0 ' no horizontal movement
        mouseEvent.dy = 0 ' no vertical movement
        mouseEvent.mouseData = 0 ' not needed
        mouseEvent.dwFlags = MOUSEEVENTF_LEFTUP ' left button up
        mouseEvent.dwtime = 0 ' use the default
        mouseEvent.dwExtraInfo = 0 ' not needed
        ' Copy the structure into the input array's buffer.
        inputEvents(1).dwType = INPUT_MOUSE ' mouse input
        CopyMemory inputEvents(1).xi(0), mouseEvent, Len(mouseEvent)
          
        ' Now that all the information for the 2 input events has been placed
        ' into the array, finally send it into the input stream.
        intX = SendInput(2, inputEvents(0), Len(inputEvents(0))) ' place the events into the stream
    
    End Sub
    Last edited by Cimperiali; August 25th, 2003 at 07:17 AM.

  2. #2
    Join Date
    Oct 2006
    Posts
    23

    Re: SendInput

    i'm not sure whether you see this or not, but with little hope i'm posting.


    hii ,
    i'm searching for how to use sendinput api , i saw you link

    i didnt understand what happens when we run the code you supplied there.

    please tell me what it does, i want to simulate mouse clicks for my project??

    i'm using VB6 and os is windows XP.

    THANKS in advance.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: SendInput

    eswar_525, welcome to the forums.

    Please note the date of the original post. This thread was created over 3.5 years ago. You should have posted your own new thread.

    For API, AllAPI.net should be your first stop. Here is the SendInput API information.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Oct 2006
    Posts
    23

    Re: SendInput

    thanks for your suggestion. Yes I know little bit about sendinput API.

    But can you tell me what happens when we run the code given there by '
    'Covin' in this post.

    Thanks in advance.

  5. #5
    Join Date
    Oct 2006
    Posts
    23

    Re: SendInput

    sorry me again..

    i wanna know more about the following

    MOUSEEVENTF_LEFTDOWN = &H2
    MOUSEEVENTF_LEFTUP = &H4
    WM_ACTIVATE
    GW_CHILD
    GW_HWNDNEXT
    MA_ACTIVATE
    ..
    ..
    ..


    can you give me a link where I can find related information about them.

    thanks.
    Last edited by eswar_525; October 26th, 2006 at 11:26 PM. Reason: wanna be more clear

  6. #6
    Join Date
    Sep 2005
    Posts
    233

    Re: SendInput

    Welcome. You should definetly check out www.AllApi.net. The website supplies the syntax of the SendInput API and a couple of examples. If your answer cannot be found there, then search microsofts MSDN library. Happy Searching!
    Seed Gaming
    www.seedgaming.com

  7. #7
    Join Date
    May 2007
    Posts
    1

    Re: SendInput

    i use VB Express Edition 2005 but anyways you guys are doing it the hard way, dont make an array of bytes make it a mouseinput structure. you can also add the constants for mousemove leftdown leftup...they are bitflags and can all be sent at once, i use short names for my constants but you should be able to figure out what they all are. keep in mind my integers are 32 bit

    Public Structure MOUSEINPUT
    Public dx As Integer
    Public dy As Integer
    Public mouseData As Integer
    Public dwFlags As Integer
    Public dwtime As Integer
    Public dwExtraInfo As Integer
    End Structure

    Public Structure INPUT_TYPE
    Public dwType As Integer
    Public xi As MOUSEINPUT
    End Structure

    Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE, ByVal cbSize As Integer) As Integer
    Const M_MOVE = &H1
    Const M_LD = &H2
    Const M_LU = &H4
    Public Sub ClickDaMouse(ByVal xyC As xyCord, ByVal iMF As Integer)

    Dim inputEvents(0) As INPUT_TYPE

    inputEvents(0).xi.dx = 0
    inputEvents(0).xi.dy = 0
    inputEvents(0).xi.mouseData = 0
    inputEvents(0).xi.dwFlags = M_MOVE + M_LD + M_LU
    inputEvents(0).xi.dwtime = 0
    inputEvents(0).xi.dwExtraInfo = 0
    inputEvents(0).dwType = INPUT_MOUSE

    SendInput(1, inputEvents(0), Len(inputEvents(0)))
    End Sub

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

Featured