A common question I get is how to send keystrokes to a running program. ______________________________________________________________________ If the program is a DOS program, you'll probably need to use a DOS keystroke TSR program. Suggestions: ScanCode: http://www.simtel.net/product.download.mirrors.php?id=3741 PC Magazine's key-fake: ftp://ftp.pcmag.com/archives/antique/antique.zip Other keyboard utilities: http://www.simtel.net/category.php?id=172 If you use ScanCode, the manual should be avoided! Why? Because it is too big! Just type "scancode /?" at a DOS prompt and read the short help screen. Most of the time, that's all the help you'll need. ______________________________________________________________________ You can send keystrokes (with mixed results depending on your operating system) to a DOS window from the Windows Scripting "CSCRIPT" program. You may need to upgrade your scripting if you have the old version that shipped on the Win 98 CDROM. Try a VBS script like this: Wscript.Stdout.WriteLine "dir" Wscript.Sleep 5000 Wscript.Stdout.WriteLine "echo on" Test the above script with a batch file like this: Cscript.exe //NoLogo test.vbs | command.com (Use cmd.exe instead of command.com on an XP/NT/2000 system) On a Win9x box, nothing will happen for 5 seconds, then the DIR and ECHO ON commands will be sent right next to each other. On a Win2000 box, DIR will be sent, then a five-second delay, then ECHO ON will be sent. Obviously this is more useful under Windows 2000! Use "Write" instead of "WriteLine" if you don't want a carriage return added to your text. And I bet you already figured out that the "Sleep" value is in milliseconds. ______________________________________________________________________ If what you're trying to do is automate the RUNAS command, you'll need a different script. Try something like this where FOO is the user name, BAR is the password, and notepad.exe is the program you want to run. Set ws = CreateObject("WScript.Shell") ws.Run "runas.exe /user:FOO notepad.exe", 1, False Wscript.Sleep 1000 ws.SendKeys "BAR" ws.SendKeys "{ENTER}" ______________________________________________________________________ For other RUNAS solutions, you might want to check out "SU.EXE" in the Windows 2000 Resource Kit (maybe in other resource kits too). In addition, you can try these programs I have absolutely no personal experience with: http://www.sysinternals.com/Utilities/PsExec.html http://www.quimeras.com/Products/products.asp http://www.joeware.net/win/free/tools/cpau.htm http://robotronic.de/runasspcEn.html ______________________________________________________________________ For a generic solution that works on DOS windows or regular Windows, you might want to look at AutoIt from http://www.hiddensoft.com/. It's a keystroke automater on steroids. ______________________________________________________________________ If you need to send keystrokes to an already-running Windows program, you can use Windows Scripting. I have a scripting web page here: http://www.ericphelps.com/scripting/index.htm You'll need to upgrade your version of the Windows Scripting Host if you have what shipped with the Windows 98 CDROM. If you have Windows 2000 or newer, you should be okay. Basically, this one-line VBS script (call it "SendKeys.vbs"): CreateObject("WScript.Shell").SendKeys Wscript.Arguments(0) can be called from a batch file like this: start /w SendKeys.vbs "blah, blah, blah" But there is a little problem... The script will basically type keystrokes, but no guarantee those keystrokes will stay in the program you want! So you may need to insure the program has focus by activating the application BEFORE you use SendKeys with the "AppActivate" method. Let's call this one "AppActivate.vbs": CreateObject("WScript.Shell").AppActivate Wscript.Arguments(0) Use it like this: start /w AppActivate.vbs "Notepad" The AppActivate must be passed the beginning, end, or complete text of a window title. "Notepad", for example, is at the end of the title bar. You use whatever is appropriate for the program you are controlling. If you need a time delay, here is a VBS script (call it Sleep.vbs): Wscript.Sleep Wscript.Arguments(0) * 1000 That will delay for as many seconds as you tell it: start /w Sleep.vbs 10 You can run each line from a batch file like I show above (using the start command each time), or you could put all those lines together in one large VBS file. If you want to put them in one large VBS file, just replace the "Wscript.Arguments(0)" part with your value like this: start /w AppActivate.vbs "Telnet" Wscript.Sleep 1*1000 CreateObject("WScript.Shell").SendKeys "username{ENTER}" Wscript.Sleep 2*1000 start /w AppActivate.vbs "Telnet" Wscript.Sleep 1*1000 CreateObject("WScript.Shell").SendKeys "password{ENTER}" When you get more advanced, you'll find you don't need to create the objects every time. But I'll leave that for you to discover if you decide to learn more about Visual Basic and Scripting. ______________________________________________________________________ Here is the help file for the special keys you need to know to use SendKeys (copied directly from the Microsoft help file): 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, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string. 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 {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify brace characters, use {{} and {}}. 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 shown below: 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} 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} 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.