Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-30
13
Medium Priority
?
6,531 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 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 400 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 800 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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

926 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