?
Solved

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

Posted on 2004-08-30
13
Medium Priority
?
6,399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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