Results 1 to 2 of 2

Thread: SendInput API - How do you use it?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    20
    I've been trying to use the SendKeys functions in a program and have come to the conclusion that it simply won't work for my application ... so I'm trying to figure out how to use the SendInput API. I've been to the VBAPI web-site, but the example they give is currently beyond my understanding. The SendInput API supposedly places events into the input stream, well what is the input stream and how do I direct it to my target application??

    My application reads a text file and then controls another application via keyboard action (tried using SendKeys), and finally sends each line of text to the other application.

    I guess I'm asking if anyone has or knows of a simple example that uses the SendInput API to send some key strokes or strings to a target application.

    Thanks for any help you can provide.

    Dave

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I just had a look at it, and it seems the sendinput function just sends the keyboard or mouse input to the active application. I had a look at it, because I wondered if it could send input to dos applications. SendKeys doesn't work with them, and it seems that sendinput can't do it either.
    I can't really give you a simple example, because I don't think it's a simple subject, but here is a slightly changed sample I found on a website. I added some constant declarations that might be usefull and for my own test I changed the mouseboard event in a simple enter key simulation.

    Code:
    Option Explicit
    Const INPUT_MOUSE = 0
    Const INPUT_KEYBOARD = 1
    Const INPUT_HARDWARE = 2
    
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Const KEYEVENTF_KEYUP = &H2
    Const KEYEVENTF_UNICODE = &H4
    
    Const MOUSEEVENTF_ABSOLUTE = &H8000
    Const MOUSEEVENTF_LEFTDOWN = &H2
    Const MOUSEEVENTF_LEFTUP = &H4
    Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Const MOUSEEVENTF_MIDDLEUP = &H40
    Const MOUSEEVENTF_MOVE = &H1
    Const MOUSEEVENTF_RIGHTDOWN = &H8
    Const MOUSEEVENTF_RIGHTUP = &H10
    Const MOUSEEVENTF_WHEEL = &H80
    Const MOUSEEVENTF_XDOWN = &H100
    Const MOUSEEVENTF_XUP = &H200
    Const WHEEL_DELTA = 120
    Const XBUTTON1 = &H1
    Const XBUTTON2 = &H2
    
    Private Type INPUT_TYPE
      dwType As Long
      xi(0 To 23) As Byte
    End Type
    Private Type KEYBDINPUT
      wVk As Integer
      wScan As Integer
      dwFlags As Long
      time As Long
      dwExtraInfo As Long
    End Type
    Private Type MOUSEINPUT
      dx As Long
      dy As Long
      mouseData As Long
      dwFlags As Long
      time As Long
      dwExtraInfo As Long
    End Type
    
    Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
    
    
    
    
    
    Private Sub Command1_Click()
    Dim tijd As Long
    tijd = Timer
    While tijd + 15 > Timer
        DoEvents
    Wend
    ' Synthesize the user typing the letter P followed by clicking
    ' the right mouse button.  Note how the information for each individual input
    ' event is placed in its associated structure before copying it into the
    ' input array.
    Dim inputevents(0 To 3) As INPUT_TYPE  ' holds information about each event
    Dim keyevent As KEYBDINPUT  ' temporarily hold keyboard input info
    Dim mouseevent As MOUSEINPUT  ' temporarily hold mouse input info
    
    ' Load the information needed to synthesize pressing the P key.
    keyevent.wVk = VkKeyScan(Asc("P")) And &HFF  ' the P key
    keyevent.wScan = 0  ' not needed
    keyevent.dwFlags = 0  ' press the key down
    keyevent.time = 0  ' use the default
    keyevent.dwExtraInfo = 0  ' not needed
    ' Copy the structure into the input array's buffer.
    inputevents(0).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory inputevents(0).xi(0), keyevent, Len(keyevent)
    
    ' Do the same as above, but for releasing the P key.
    keyevent.wVk = VkKeyScan(Asc("P")) And &HFF ' the P key
    keyevent.wScan = 0  ' not needed
    keyevent.dwFlags = KEYEVENTF_KEYUP  ' release the key
    keyevent.time = 0  ' use the default
    keyevent.dwExtraInfo = 0  ' not needed
    inputevents(1).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory inputevents(1).xi(0), keyevent, Len(keyevent)
    
    ' Load the information needed to synthesize pressing enter.
    keyevent.wVk = VkKeyScan(13) And &HFF  ' cr
    keyevent.wScan = 0  ' not needed
    keyevent.dwFlags = 0  ' press the key down
    keyevent.time = 0  ' use the default
    keyevent.dwExtraInfo = 0  ' not needed
    ' Copy the structure into the input array's buffer.
    inputevents(2).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory inputevents(2).xi(0), keyevent, Len(keyevent)
    
    ' Do the same as above, but for releasing the enter key.
    keyevent.wVk = VkKeyScan(13) And &HFF ' cr
    keyevent.wScan = 0  ' not needed
    keyevent.dwFlags = KEYEVENTF_KEYUP  ' release the key
    keyevent.time = 0  ' use the default
    keyevent.dwExtraInfo = 0  ' not needed
    inputevents(3).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory inputevents(3).xi(0), keyevent, Len(keyevent)
    
    '' Load the information needed to synthesize pressing the right mouse button.
    'mouseevent.dx = 0  ' no horizontal movement
    'mouseevent.dy = 0  ' no vertical movement
    'mouseevent.mouseData = 0  ' not needed
    'mouseevent.dwFlags = MOUSEEVENTF_RIGHTDOWN  ' right button down
    'mouseevent.time = 0  ' use the default
    'mouseevent.dwExtraInfo = 0  ' not needed
    '' Copy the structure into the input array's buffer.
    'inputevents(2).dwType = INPUT_MOUSE  ' mouse input
    'CopyMemory inputevents(2).xi(0), mouseevent, Len(mouseevent)
    '
    '' Do the same as above, but for releasing the right mouse button.
    'mouseevent.dx = 0  ' no horizontal movement
    'mouseevent.dy = 0  ' no vertical movement
    'mouseevent.mouseData = 0  ' not needed
    'mouseevent.dwFlags = MOUSEEVENTF_RIGHTUP  ' right button up
    'mouseevent.time = 0  ' use the default
    'mouseevent.dwExtraInfo = 0  ' not needed
    '' Copy the structure into the input array's buffer.
    'inputevents(3).dwType = INPUT_MOUSE  ' mouse input
    'CopyMemory inputevents(3).xi(0), mouseevent, Len(mouseevent)
    
    ' Now that all the information for the four input events has been placed
    ' into the array, finally send it into the input stream.
    SendInput 4, inputevents(0), Len(inputevents(0)) ' place the events into the stream
    
    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