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 event is placed in its associated structure before copying it into the input array. This example runs when the user clicks button Command1. So, to run this example, you must first place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type MOUSEINPUT
      dx As Long
      dy As Long
      mouseData As Long
      dwFlags As Long
      time As Long
      dwExtraInfo As Long
End Type
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Type KEYBDINPUT
      wVk As Integer
      wScan As Integer
      dwFlags As Long
      time As Long
      dwExtraInfo As Long
End Type
Public Const VK_P = &H50  ' using vbKeyP instead would also work
Public Const KEYEVENTF_KEYUP = &H2
Public Type INPUT_TYPE
      dwType As Long
      xi(0 To 23) As Byte
End Type
Public Const INPUT_KEYBOARD = 1
Public Const INPUT_MOUSE = 0
Public Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, _
	ByVal cbSize As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
	As Any, ByVal Length As Long)

' *** Place the following code inside the form window. ***

Private Sub Command1_Click()
	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 imitate pressing the P key.
	With keyevent
		.wVk = VK_P       ' the P key
		.wScan = 0        ' not needed
		.dwFlags = 0      ' press the key down
		.time = 0         ' use the default
		.dwExtraInfo = 0  ' not needed
	End With
	' Copy the structure into the input array's buffer.
	inputevents(0).dwType = INPUT_KEYBOARD
	CopyMemory inputevents(0).xi(0), keyevent, Len(keyevent)
	
	' Do the same as above, but for releasing the P key.
	With keyevent
		.wVk = VK_P       ' the P key
		.wScan = 0        ' not needed
		.dwFlags = KEYEVENTF_KEYUP  ' release the key
		.time = 0         ' use the default
		.dwExtraInfo = 0  ' not needed
	End With
	inputevents(1).dwType = INPUT_KEYBOARD
	CopyMemory inputevents(1).xi(0), keyevent, Len(keyevent)
	
	' Load the information needed to imitate pressing the right mouse button.
	With mouseevent
		.dx = 0            ' no horizontal movement
		.dy = 0            ' no vertical movement
		.mouseData = 0     ' not needed
		.dwFlags = MOUSEEVENTF_RIGHTDOWN  ' right button down
		.time = 0          ' use the default
		.dwExtraInfo = 0   ' not needed
	End With
	' Copy the structure into the input array's buffer.
	inputevents(2).dwType = INPUT_MOUSE
	CopyMemory inputevents(2).xi(0), mouseevent, Len(mouseevent)
	
	' Do the same as above, but for releasing the right mouse button.
	With mouseevent
		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
	End With
	' Copy the structure into the input array's buffer.
	inputevents(3).dwType = INPUT_MOUSE
	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))
End Sub

See Also

keybd_event, mouse_event

Category

Input (General)

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/sendinput.html