The SendKeys method is used to send keystrokes to the currently active window as if they where typed from the keyboard.
object.SendKeys Keystrokes
Single alphanumeric keystrokes can simply be specified using a string representation of the character required. For example, to send the letter S the command would be object.SendKeys "S".
To send multiple characters combine them into one string. For example, to send A, B and C the command would be object.SendKeys "ABC".
The plus sign "+", caret "^", percent sign "%", tilde "~", and parentheses "()" all have special meanings and must be enclosed within braces "{}". Square brackets "[]" must also be enclosed within braces although they have no special meaning. To specify brace characters themselves, use "{{}" and "{}}".
Below is a table of characters that can not be directly represent by a keyboard character to use one of these, specify the appropriate Code.

KeyCode
Backspace{BACKSPACE}, {BKSP} or {BS}
Break{BREAK}
Caps Lock{CAPSLOCK}
Delete{DELETE} or {DEL}
Down Arrow{DOWN}
End{END}
Enter{ENTER} or ~
Escape{ESC}
Help{HELP}
Home{HOME}
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 characters combinations use the following codes:

KeyCode
Alt%
Ctrl^
Shift Lock+
For example to specify CTRL and C, the code would be object.SendKeys "^C" and for SHIFT F5 object.SendKeys "+{F5}". To specify multiple combination sets such as ALT A Z, you use parentheses, for example, object.SendKeys "%(AZ)".

Example

'VBScript ExampleSet WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"

WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "abc"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "def"

-----------------------------------------------------------------

Some examples of using sendkeys are shown below:
Example 1: Open command prompt and send some keystrokes
Set objKey = CreateObject(“WScript.Shell”) objKey.Run “cmd” objKey.AppActivate “cmd” WScript.Sleep 10000 objKey.SendKeys “dir” WScript.Sleep 10000 objKey.SendKeys “{ENTER}”Run the above code in a vbs file. It will open command prompt and send “dir” keys and then send the “Enter” key.
WScript provides a function called Sleep which is used to wait for specified milliseconds. In our script, we are waiting for 10000 milliseconds or 10 seconds(10000 milliseconds=10 seconds).
Try to run the above code in QTP. It will error out as WScript is a host specific object which is not recognized by QTP. To run the above code in QTP, replace the WScript.Sleep statement with wait statement.
The modified code is shown below:
Set objKey = CreateObject(“WScript.Shell”) objKey.Run “cmd” objKey.AppActivate “cmd” wait (2) objKey.SendKeys “dir” wait (2) objKey.SendKeys “{ENTER}”
Example 2: Using Sendkeys to send keystrokes to Calculator
Set objKey = CreateObject(“WScript.Shell”) Set objCalc=Window(“nativeclass:=SciCalc”, “index:=0”) SystemUtil.Run “calc.exe” wait (4) objKey.AppActivate “Calculator” objKey.SendKeys “2{+}” wait(2) objKey.SendKeys “2” wait(2) objKey.SendKeys “=”
We have seen that in order to press enter, we can use objKey.SendKeys “{ENTER}”. However, there are two more ways to press enter using Sendkeys.
1. Use the Tilde character: ~
We can use in the following way: obj.Sendkeys (“~”)
2. Use the ASCII character for Enter or carriage return which is Chr(13)
obj.SendKeys (Chr(13))
Please note that in this method, we can not use double quotes
Using Sendkeys, we can even send repeated keys to an application. For example,
obj.SendKeys “{D 20}” ‘Sends ‘D’ character 20 times
obj.SendKeys “+{TAB 5}” ‘Simulates SHIFT+TAB 5 times
The following chart illustrates the symbols for Control, Alt and shift keys
KeystrokeKeyboard Symbol
Ctrl^
Alt%
Shift+
Also, we can simulate sending space key using the below code:
obj.SendKeys ” ”
We can also use Sendkeys to send right mouse click using QTP on some windows which supports simulating right mouse clicking using Shift and F10 key.
We can use the below code for the same:
obj.SendKeys “+{F10}”
===============================================
WSH (Windows Scripting Host) is a powerful scripting environment that comes with Windows Operating System. By default, there are two scripting languages supported, which is JScript and VBScript. You could actually open any text editor and write the scripts and save as *.vbs or *.js. Double click them invokes the interpreter, which is cscript.exe or wscript.exe depending on the settings. The cscript.exe will output to console (e.g. using WScript.Echo) while the wscript.exe will treat WScript.Echo as MsgBox (output using dialog). Sometimes, you could choose other languages as the host languages in WSH, but this is not installed by default, and you have to download corresponding installer.
The advantages of writing scripts to run in WSH are: (1) you do not need to compile them, just save and run immediately, which are suitable for lightweight small and daily tasks. (2) the scripts can be edited/changed and distributed easily. (3) the same scripts can be interpreted for both 32 and 64 bit, just specify the corresponding cscript.exe or wscript.exerespectively.
You could actually use WScript.Shell object (COM Automation) to SendKeys to the active window, in which case, you could do something as powerful as macros. The keystrokes are being sent to the active window.
The following runs the notepad.exe and bring the notepad to the front. The second parameter 9 means: to active and display the window. If the window is minimized or maximized, the system restores it to its original size and position.
1
2
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 9 
Now the following will type in Hello, World! one character after another, with a short pause 200ms between characters. And finally, it will simulate Alt+F4 to exit. At this time, the dialog will pop up for saving, and press Tab to navigate to Don’t Save button and press Enter to exit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
' Give Notepad time to load
WScript.Sleep 500 
 
Dim Msg: Msg = "Hello, World!"
 
' Type in One Character at a time
For i = 1 To Len(Msg)
    WScript.Sleep 200
    WshShell.SendKeys Mid(Msg, i, 1)
Next
 
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "%{F4}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
notepad-sendkey-vbs Send Keystrokes to the Active Window using SendKeys (WSH) code code library implementation javascript programming languages VBA vbscript windows windows scripting host
The specified keys, functional keys, ctrl, alt are enclosed by {braces}.
vbs-sendkeys Send Keystrokes to the Active Window using SendKeys (WSH) code code library implementation javascript programming languages VBA vbscript windows windows scripting host
You can combine keys, for example, + (plus) is the prefix for shift, Ctrl is denoted using prefix ^ and % (percentage) is for ALT.
Let’s review the above example one more time, but in JScript.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(function () {
          var WshShell = WScript.CreateObject("WScript.Shell");
          WshShell.Run("notepad", 9);
          var msg = "Hello, World!";
          // Give Notepad time to load
          WScript.Sleep(500); 
          for (i = 0; i < msg.length; i ++) {
              WScript.Sleep(200);
              WshShell.SendKeys(msg.charAt(i));
          }
          WshShell.SendKeys("{ENTER}");
          // Alt + F4
          WshShell.SendKeys("%{F4}");
          // Naviagate to Don't Save
          WshShell.SendKeys("{TAB}");
          // Exit
          WshShell.SendKeys("{ENTER}");          
})();


======================================================
Example
' Open notepad 
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 9

' Give Notepad time to load
WScript.Sleep 500 

' Type in Hello World
WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"

' Add the date
WshShell.SendKeys "{F5}"