"send keys" to another app from vb.net?

Does anyone know an easy way to send keys to another window from vb.net?

For example, let's say that I programmatically open application X from vb.net.  Application X has a popup box that asks for a password.  I want to automatically insert the password into that popup box from vb.net.

Who is Participating?
Daniellus83Connect With a Mentor Commented:
Hello, I've worked much with keystrokes! (still do...). Wguerram has given a nice example, but I wanted to add this URL;


Here you find an exact overview of all useable combinations. You wil also need for your solution the {TAB} key.

Here is the part I ve copied from the website:

Use SendKeys to send keystrokes and keystroke combinations to the active application. This class cannot be instantiated. To send a keystroke to a class and immediately continue with the flow of your program, use Send. To wait for any processes started by the keystroke, use SendWait.

Each key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, pass in the string "A" to the method. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, specify the parameter as "ABC".

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use "{+}". To specify brace characters, use "{{}" and "{}}". Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that might be significant when dynamic data exchange (DDE) occurs.

To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes in the following table.

Key Code
BREAK             {BREAK}
END                {END}
ENTER            {ENTER}or ~
ESC                {ESC}
HELP               {HELP}
HOME             {HOME}
PAGE UP          {PGUP}
PRINT SCREEN {PRTSC} (reserved for future use)
TAB                {TAB}
UP ARROW      {UP}
F1                   {F1}
F2                   {F2}
F3                   {F3}
F4                   {F4}
F5                   {F5}
F6                   {F6}
F7                   {F7}
F8                   {F8}
F9                   {F9}
F10                 {F10}
F11                 {F11}
F12                 {F12}
F13                 {F13}
F14                 {F14}
F15                 {F15}
F16                 {F16}
Keypad add      {ADD}
Keypad subtract   {SUBTRACT}
Keypad multiply   {MULTIPLY}
Keypad divide      {DIVIDE}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes.

Key Code

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.

Note   Because there is no managed method to activate another application, you can either use this class within the current application or use native Windows methods, such as FindWindow and SetForegroundWindow, to force focus on other applications.
In The MSDN:

Dim ReturnValue, I
ReturnValue = Shell("calc.exe", 1)   ' Run Calculator.
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.

Keys are processed by active application
Take a look at this:

This find a window and activates, the close the form

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

iboutchkineConnect With a Mentor Commented:

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

        Dim ReturnValue, I
        ReturnValue = Shell("calc.exe", 1)   ' Run Calculator.
        AppActivate(ReturnValue)   ' Activate the Calculator.
        For I = 1 To 10   ' Set up counting loop.
            SendKeys.Send(I & "{+}") ' Send keystrokes to Calculator
        Next I   ' to add each value of I.
        SendKeys.Send("=")  ' Get grand total.
        SendKeys.Send("%{F4}")  ' Send ALT+F4 to close Calculator.
born4codeAuthor Commented:
Okay guys... I'm in a hotel w/laptop... let me try this out and I'll get back to you...

gajendraConnect With a Mentor Commented:
Let me add....

SendKeys  is equivalent to typing the keys from the keyboard. The keys typed at the keyboard are always received by the window or contron in focus.

To "SendKeys" to another application simply set focus to the application and Send the Keys

Hope this helps

born4codeAuthor Commented:
I've tried the sendkeys method... actually it appears that I have user SendKeys.Send in .net

Unfortunately, while it works great in something like the windows calculator, it is ignored by some applications.  Not really sure why.  One of the first keys I want to send is just the UP arrow, i.e., SendKeys("{UP}"), but the application just ignores it.  I'm almost wondering if the splash screen of the application still has the focus when the application starts up, and therefore it cannot receive the keys because it does not have focus.  Perhaps if there was a way to "stall" for a few seconds before I sent the keys it would work.

Like I said - it is not actually ignored.

Just that the right control is not in focus.

born4codeAuthor Commented:
So, let's say that I fire up an application with a shell command... the application shows up immediatly, takes place as the first window on the pc.  So then one of these below must apply.... which one do you think it is?


1) It does not have focus, even though it was just called and exists as the first window.  This is because...

2) It does have focus, but the keys are being ignored because...

3) The splash screen of the application has focus first, so the sendkeys code is being intercepted by the splash screen.  A way to get around this would be to...

Try this

'"Personal Folders Password" is the name of my oulook password window.

AppActivate("Personal Folders Password")        'This activates the window which is waiting for a password to be inputed
SendKeys.SendWait("MyPassword{ENTER}")    'Then the password is written
I mean by name, the caption title of the window.

"Personal Folders Password"
gajendraConnect With a Mentor Commented:
You could even consider sending something like %P (Alt P - assuming P underlined) to ensure that the proper control is in focus.

All guys wrote comments and provide links on a common task and all of then received points but not me.

All of them gave you the same comments as mine.

Now the question is, why didn't i received points on this?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.