PDA

View Full Version : SendInput - Using the 'Enter' Command



sara2013
12-16-2012, 08:26 AM
Hi Everyone,

Could someone please advise as to how I can send an 'Enter' key press using the following code:

Private Declare Function SendInput Lib "user32.dll" _ (ByVal nInputs As Long, ByRef pInputs As Any, _ ByVal cbSize As Long) As Long Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" _ (ByVal cChar As Byte) As Integer Private Type KeyboardInput ' typedef struct tagINPUT { dwType As Long ' DWORD type; wVK As Integer ' union {MOUSEINPUT mi; wScan As Integer ' KEYBDINPUT ki; dwFlags As Long ' HARDWAREINPUT hi; dwTime As Long ' }; dwExtraInfo As Long ' }INPUT, *PINPUT; dwPadding As Currency ' 8 extra bytes, because mouses take more. End Type Private Const INPUT_MOUSE As Long = 0 Private Const INPUT_KEYBOARD As Long = 1 Private Const KEYEVENTF_KEYUP As Long = 2 Private Const VK_LSHIFT = &HA0 Public Sub SendKey(ByVal Data As String) Dim ki() As KeyboardInput Dim i As Long Dim o As Long ' output buffer position Dim c As String ' character ReDim ki(1 To Len(Data) * 4) As KeyboardInput o = 1 For i = 1 To Len(Data) c = Mid$(Data, i, 1) Select Case c Case "A" To "Z": ' upper case ki(o).dwType = INPUT_KEYBOARD 'shift down ki(o).wVK = VK_LSHIFT ki(o + 1) = ki(o) ' key down ki(o + 1).wVK = VkKeyScan(Asc(c)) ki(o + 2) = ki(o + 1) ' key up ki(o + 2).dwFlags = KEYEVENTF_KEYUP ki(o + 3) = ki(o) ' shift up ki(o + 3).dwFlags = KEYEVENTF_KEYUP o = o + 4 Case Else: ' lower case ki(o).dwType = INPUT_KEYBOARD ki(o).wVK = VkKeyScan(Asc(c)) ki(o + 1) = ki(o) ki(o + 1).dwFlags = KEYEVENTF_KEYUP o = o + 2 End Select Next i Debug.Print SendInput(o - 1, ki(1), LenB(ki(1))), 'Debug.Print Err.LastDllError End Sub Private Sub Command1_Click() Text1.Text = "" Text1.SetFocus DoEvents Call SendKey("This Is A Test") End Sub

Any assistance is greatly appreciated.

Sara

mikerickson
12-16-2012, 09:00 AM
Send it to where?

sara2013
12-16-2012, 11:20 AM
Currently I would like to send the SendInput command to notepad, however if I am able to get it running I would be sending the keyboard commands to a bespoke software that we use. For example, it could be used to change a client address so instead of typing it directly, the SendInput would send the text directly.

snb
12-16-2012, 01:34 PM
There are better ways to create a textfile using VBA.
Please tell us why you want to create a textfile; what will be done with the result, which information should it contain ?

Kenneth Hobs
12-17-2012, 06:59 AM
When pasting code between vba code tags, you may need to first past to an Excel range or other and recopy and paste to get it to display properly.

If reading and writing to a text file is the goal, snb's suggestion should be used.

Here is one thread for using API's to work with Notepad. http://www.vbaexpress.com/forum/showthread.php?t=43658

I could show you how to use SendInput but why not just use SendKeys()? You would need to disable UAC to get SendKeys() to work though SendKeys() methods should be your last choice. e.g.
Sub SavePartcorrect()
Dim myPath As String, txtPath As String
Dim rc As Long
Dim wb As Workbook

Set wb = ActiveWorkbook
myPath = ThisWorkbook.Path & "\"
txtPath = myPath & "Test.txt"

rc = Shell("NOTEPAD.EXE " & txtPath, vbNormalFocus)
AppActivate rc
Application.Wait Now + TimeValue("00:00:01")
SendKeys Application.UserName, True
SendKeys "{Enter}", True
End Sub

Here is an example using SendInput. http://www.mrexcel.com/forum/excel-questions/411552-sendinput-visual-basic-applications.html

rcharters
12-17-2012, 01:12 PM
You can use this code to send the Enter Key


Private Const VK_RETURN As Long = &HD

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Sub ReturnKey()
keybd_event VK_RETURN, 0, 0, 0
End Sub

Jayme65
02-05-2013, 01:08 AM
You can use this code to send the Enter Key


Private Const VK_RETURN As Long = &HD

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Sub ReturnKey()
keybd_event VK_RETURN, 0, 0, 0
End Sub


When using this code I get a

A call to PInvoke function 'SampleMethod' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Would you please tell me what I should do?

rcharters
02-06-2013, 10:26 AM
What version of MS Office are you running 32 or 64 bit?