Solved

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

Posted on 2004-08-30
13
5,776 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Author Comment

by:born4code
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now