Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


VB 6 "SendKeys" not working in application

Posted on 2003-03-04
Medium Priority
Last Modified: 2012-08-14
I'm trying to make an automation tool in VB 6 that will grab the client's .exe and send it information via the SendKeys function.

This is not working however.

Is there a simple API call to force input of the data?  Please provide an example.
Question by:heroTop

Expert Comment

ID: 8068937
Try doing it this way (straight from MSDN):

Dim ReturnValue, I
ReturnValue = Shell("calc.exe", 1)   ' Run Calculator.
'above you would run the client program
AppActivate ReturnValue    ' Activate the Calculator.
For I = 1 To 100   ' Set up counting loop.
   SendKeys I & "{+}", True   ' Send keystrokes to Calculator
Next I   ' to add each value of I.
SendKeys "=", True   ' Get grand total.
SendKeys "%{F4}", True   ' Send ALT+F4 to close Calculator.

Or Instead of Running the clients .exe for them, use the name of the client in the appactivate like this:

Private Sub Command1_Click()
On Error GoTo ErrorPro
AppActivate "Untitled - Notepad"    ' Activate the Program by Name in Title
SendKeys "Hi my name is johnny", True   ' Send Text
Exit Sub
MsgBox "The Program is not open, please open it and try again"
End Sub

Author Comment

ID: 8068954
I tried this same code from the MSDN and it didn' work.

It will work fine if my client application is "Notepad", but my work client application ignores the sent keys...
LVL 18

Expert Comment

ID: 8069047
you could try keybd_event api which basically simulates keystrokes:

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

'' some constants you will probably need (sorry for the language difference)
Const  VK_LBUTTON = &H1 'Linker Mausbutton
Const  VK_RBUTTON = &H2 'Rechter Mausbutton
Const  VK_MBUTTON = &H4 'Mittlerer Masubutton
Const  VK_BACK = &H8 'Backspace Taste
Const  VK_TAB = &H9 'Tab Taste
Const  VK_CLEAR = &HC 'Numpad 5 ohne Numlock
Const  VK_RETURN = &HD 'Enter Taste
Const  VK_SHIFT = &H10 'Shift Taste
Const  VK_CONTROL = &H11 'STRG Taste
Const  VK_MENU = &H12 'Alt Taste
Const  VK_PAUSE = &H13 'Pause/Untbr
Const  VK_CAPITAL = &H14 'Caps Lock/Feststelltaste
Const  VK_ESCAPE = &H1B 'Escape
Const  VK_SPACE = &H20 'Space/Leertaste
Const  VK_PRIOR = &H21 'PageUp/Bild hoch
Const  VK_NEXT = &H22 'PageDown/Bild runter
Const  VK_END = &H23 'Ende
Const  VK_HOME = &H24 'Home/Pos1
Const  VK_LEFT = &H25 'Linke Pfeiltaste
Const  VK_UP = &H26 'Obere Pfeilaste
Const  VK_RIGHT = &H27 'Rechte Pfeiltaste
Const  VK_DOWN = &H28 'Untee Pfeiltaste
Const  VK_PRINT = &H2A 'Drucken (Nokia Tastaturen)
Const  VK_SNAPSHOT = &H2C 'Drucken/S-Abf
Const  VK_INSERT = &H2D 'Einf|gen
Const  VK_DELETE = &H2E 'Entfernen
Const  VK_HELP = &H2F 'Hilfe
Const  VK_0 = &H30 'Taste 0
Const  VK_1 = &H31 'Taste 1
Const  VK_2 = &H32 'Taste 2
Const  VK_3 = &H33 'Taste 3
Const  VK_4 = &H34 'Taste 4
Const  VK_5 = &H35 'Taste 5
Const  VK_6 = &H36 'Taste 6
Const  VK_7 = &H37 'Taste 7
Const  VK_8 = &H38 'Taste 8
Const  VK_9 = &H39 'Taste 9
Const  VK_A = &H41 'Taste A
Const  VK_B = &H42 'Taste B
Const  VK_C = &H43 'Taste C
Const  VK_D = &H44 'Taste D
Const  VK_E = &H45 'Taste E
Const  VK_F = &H46 'Taste F
Const  VK_G = &H47 'Taste G
Const  VK_H = &H48 'Taste H
Const  VK_I = &H49 'Taste I
Const  VK_J = &H4A 'Taste J
Const  VK_K = &H4B 'Taste K
Const  VK_L = &H4C 'Taste L
Const  VK_M = &H4D 'Taste M
Const  VK_N = &H4E 'Taste N
Const  VK_O = &H4F 'Taste O
Const  VK_P = &H50 'Taste P
Const  VK_Q = &H51 'Taste Q
Const  VK_R = &H52 'Taste R
Const  VK_S = &H53 'Taste S
Const  VK_T = &H54 'Taste T
Const  VK_U = &H55 'Taste U
Const  VK_V = &H56 'Taste V
Const  VK_W = &H57 'Taste W
Const  VK_X = &H58 'Taste X
Const  VK_Y = &H59 'Taste Y
Const  VK_Z = &H5A 'Taste Z
Const  VK_STARTKEY = &H5B 'Startmen|taste
Const  VK_CONTEXTKEY = &H5D 'Kentextmen|
Const  VK_NUMPAD0 = &H60 'Numpad Taste 0
Const  VK_NUMPAD1 = &H61 'Numpad Taste 1
Const  VK_NUMPAD2 = &H62 'Numpad Taste 2
Const  VK_NUMPAD3 = &H63 'Numpad Taste 3
Const  VK_NUMPAD4 = &H64 'Numpad Taste 4
Const  VK_NUMPAD5 = &H65 'Numpad Taste 5
Const  VK_NUMPAD6 = &H66 'Numpad Taste 6
Const  VK_NUMPAD7 = &H67 'Numpad Taste 7
Const  VK_NUMPAD8 = &H68 'Numpad Taste 8
Const  VK_NUMPAD9 = &H69 'Numpad Taste 9
Const  VK_MULTIPLY = &H6A 'Numpad Multiplikations Taste (*)
Const  VK_ADD = &H6B 'Numpad Additions Taste (+)
Const  VK_SUBTRACT = &H6D 'Numpad Subtrations Taste (-)
Const  VK_DECIMAL = &H6E 'Numpad Komma Taste (,)
Const  VK_DIVIDE = &H6F 'Numpad Devidierungs Taste (/)
Const  VK_F1 = &H70 'F1 Taste
Const  VK_F2 = &H71 'F2 Taste
Const  VK_F3 = &H72 'F3 Taste
Const  VK_F4 = &H73 'F4 Taste
Const  VK_F5 = &H74 'F5 Taste
Const  VK_F6 = &H75 'F6 Taste
Const  VK_F7 = &H76 'F7 Taste
Const  VK_F8 = &H77 'F8 Taste
Const  VK_F9 = &H78 'F9 Taste
Const  VK_F10 = &H79 'F10 Taste
Const  VK_F11 = &H7A 'F11 Taste
Const  VK_F12 = &H7B 'F12 Taste
Const  VK_F13 = &H7C 'F13 Taste
Const  VK_F14 = &H7D 'F14 Taste
Const  VK_F15 = &H7E 'F15 Taste
Const  VK_F16 = &H7F 'F16 Taste
Const  VK_F17 = &H80 'F17 Taste
Const  VK_F18 = &H81 'F18 Taste
Const  VK_F19 = &H82 'F19 Taste
Const  VK_F20 = &H83 'F20 Taste
Const  VK_F21 = &H84 'F21 Taste
Const  VK_F22 = &H85 'F22 Taste
Const  VK_F23 = &H86 'F23 Taste
Const  VK_F24 = &H87 'F24 Taste
Const  VK_NUMLOCK = &H90 'Numlock Taste
Const  VK_OEM_SCROLL = &H91 'Scroll Lock
Const  VK_LSHIFT = &HA0 'Linke Shift-Taste
Const  VK_RSHIFT = &HA1 'Rechte Shift-Taste
Const  VK_LCONTROL = &HA2 'Linke STRG-Taste
Const  VK_RCONTROL = &HA3 'Rechte STRG-Taste
Const  VK_LMENU = &HA4 'Linke ALT-Taste
Const  VK_RMENU = &HA5 'Rechte ALT-Taste
Const  VK_OEM_1 = &HBA '";"-Taste
Const  VK_OEM_PLUS = &HBB '"
Const  VK_OEM_COMMA = &HBC '","-Taste
Const  VK_OEM_MINUS = &HBD '"-"-Taste
Const  VK_OEM_PERIOD = &HBE '"."-taste
Const  VK_OEM_2 = &HBF '"/"-Taste
Const  VK_OEM_3 = &HC0 '"`"-Taste
Const  VK_OEM_4 = &HDB '"["-Taste
Const  VK_OEM_5 = &HDC '"\"-Taste
Const  VK_OEM_6 = &HDD '"]"-Taste
Const  VK_OEM_7 = &HDE '"
Const  VK_ICO_F17 = &HE0 'F17 einer Olivette Tastatur (Intern)
Const  VK_ICO_F18 = &HE1 'F18 einer Olivette Tastatur (Intern)
Const  VK_OEM102 = &HE2 '"<"-Taste oder "|"-Taste einer IBM-Kompatiblen 102 Tastatur (Nicht US)
Const  VK_ICO_HELP = &HE3 'Hilfetaste einer Olivetti Tastatur (Intern)
Const  VK_ICO_00 = &HE4 '00-Taste einer Olivetti Tastatur (Intern)
Const  VK_ICO_CLEAR = &HE6 'Lvschen Taste einer Olivetti Tastatur (Intern)
Const  VK_OEM_RESET = &HE9 'Reset Taste (Nokia)
Const  VK_OEM_JUMP = &HEA 'Springen Taste (Nokia)
Const  VK_OEM_PA1 = &HEB 'PA1 Taste (Nokia)
Const  VK_OEM_PA2 = &HEC 'PA2 Taste (Nokia)
Const  VK_OEM_PA3 = &HED 'PA3 Taste (Nokia)
Const  VK_OEM_WSCTRL = &HEE 'WSCTRL Taste (Nokia)
Const  VK_OEM_CUSEL = &HEF 'WSCTRL Taste (Nokia)
Const  VK_OEM_ATTN = &HF0 'ATTN Taste (Nokia)
Const  VK_OEM_FINNISH = &HF1 'Fertig Taste (Nokia)
Const  VK_OEM_COPY = &HF2 'Kopieren Taste (Nokia)
Const  VK_OEM_AUTO = &HF3 'Auto Taste (Nokia)
Const  VK_OEM_ENLW = &HF4 'ENLW Taste (Nokia)
Const  VK_OEM_BACKTAB = &HF5 'BackTab Taste (Nokia)
Const  VK_ATTN = &HF6 'ATTN-Taste
Const  VK_CRSEL = &HF7 'CRSEL-Taste
Const  VK_EXSEL = &HF8 'EXSEL-Taste
Const  VK_EREOF = &HF9 'EREOF-Taste
Const  VK_PLAY = &HFA 'PLAY-Taste
Const  VK_ZOOM = &HFB 'ZOOM-Taste
Const  VK_PA1 = &HFD 'PA1-Taste

public sub command1_click()

'' simulate key press of 'abc'
keybd_event VK_A, 0, 0, 0
keybd_event VK_B, 0, 0, 0
keybd_event VK_C, 0, 0, 0

end sub
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 8069133
Is it the program not accepting sentkey's or is it that when the form is activated (like switched to by Alt-TAB, or by AppActivate) that nothing that accepts text is in focus (like a textbox), because if the textbox is not in focus, u must use SendKeys "{TAB}" until it is in focus, and if the programmer of the clients exe disabled the TAB switching focus, you must find another way of giving it focus.
Do you understand what i just said lol?

Author Comment

ID: 8075326
The API call didn't work either...

It works fine in the test applications; calc, notepad, word, etc. but not the client application I need to automate.

I have tried sending keystrokes so that the appropriate control gains focus for text, but no luck.

I know it can be done as I have other macro programs that work fine.  I cannot use these as the engine for my application however...  

If I physically click the mouse in the appopriate text box before the sendkey occurs, THEN the text is sent fine.

Author Comment

ID: 8075352
A simple SendKeys "{ENTER}" should send focus to the correct control on the application but it is not.

If I click the mouse in the appropriate control and then invoke the send key the client recieves the sent keystrokes fine...
LVL 49

Expert Comment

ID: 9046885
Hi heroTop,
It appears that you have forgotten to close this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

heroTop, Please DO NOT accept THIS comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer

Accepted Solution

YensidMod earned 0 total points
ID: 9151055
Question is PAQ'd and points refunded.

Community Support Moderator @Experts Exchange

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question