Solved

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

Posted on 2004-08-30
13
5,997 Views
Last Modified: 2012-06-21
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.

Thanks.
0
Comment
Question by:born4code
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 11936420
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
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11936497
Take a look at this:

This find a window and activates, the close the form

http://www.mentalis.org/apilist/FindWindow.shtml
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 200 total points
ID: 11941064
Hello, I've worked much with keystrokes! (still do...). Wguerram has given a nice example, but I wanted to add this URL;

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformssendkeysclasstopic.asp

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
BACKSPACE     {BACKSPACE}, {BS}, or {BKSP}
BREAK             {BREAK}
CAPS LOCK      {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END                {END}
ENTER            {ENTER}or ~
ESC                {ESC}
HELP               {HELP}
HOME             {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW   {LEFT}
NUM LOCK       {NUMLOCK}
PAGE DOWN    {PGDN}
PAGE UP          {PGUP}
PRINT SCREEN {PRTSC} (reserved for future use)
RIGHT ARROW {RIGHT}
SCROLL LOCK  {SCROLLLOCK}
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
SHIFT +
CTRL ^
ALT %

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.
---------------------------------------------------------------------------------------------------------------------------------------------
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 100 total points
ID: 11941425
wquerram

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.


use
        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.
0
 
LVL 2

Author Comment

by:born4code
ID: 11948378
Whew!
Okay guys... I'm in a hotel w/laptop... let me try this out and I'll get back to you...

0
 
LVL 3

Assisted Solution

by:gajendra
gajendra earned 200 total points
ID: 11949353
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

Gajendra
0
 
LVL 2

Author Comment

by:born4code
ID: 11966625
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.

thoughts?
0
 
LVL 3

Expert Comment

by:gajendra
ID: 11970162
Like I said - it is not actually ignored.

Just that the right control is not in focus.

Gajendra
0
 
LVL 2

Author Comment

by:born4code
ID: 11976733
ok.
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?

Perhaps...

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...

Thanks.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11977231
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
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11977238
I mean by name, the caption title of the window.

"Personal Folders Password"
0
 
LVL 3

Assisted Solution

by:gajendra
gajendra earned 200 total points
ID: 11978716
You could even consider sending something like %P (Alt P - assuming P underlined) to ensure that the proper control is in focus.

Gajendra
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12006840
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?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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