SendInput Function

Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long

Platforms

Description & Usage

SendInput synthesizes a series of keyboard, mouse, or other hardware inputs and adds them the input stream. The events generated by the function are not intersperced with any other input messages, user-created or otherwise.

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns the number of input events which were successfully added to the input stream.

Visual Basic-Specific Issues

None.

Parameters

nInputs
The number of elements in the array passed as pInputs.
pInputs
An array holding information about each input event to insert into the input stream. Each element corresponds to a single input event.
cbSize
The size in bytes of a single INPUT_TYPE structure (not the total size of the array passed as pInputs.

Example

' 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 = VK_P  ' 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 = VK_P  ' 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 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.dwTime = 0  ' use the default
mouseevent.dwExtraInfo = 0  ' not needed
' Copy the structure into the input array's buffer.
inputarray(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.dwTime = 0  ' use the default
mouseevent.dwExtraInfo = 0  ' not needed
' Copy the structure into the input array's buffer.
inputarray(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, inputarray(0), Len(inputarray(0))  ' place the events into the stream

Related Functions

keybd_event, mouse_event

Category

Keyboard (Hybrid: Mouse)

Back to the index.


Last Modified: August 7, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information

Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/s/sendinput.html