VBScript Sendkeys Tutorial for QTP 11.0
Most of the applications like Microsoft Word or Microsoft excel have exposed their object model so that we can directly interact with these applications through their COM interface by instantiating them and later we can access all methods and properties.
The only catch here is that we should know all the methods and properties of the corresponding object. Sometimes, it could also happen that the object model is not exposed by the vendor. But you can still do away with this limitation by using VBScript Sendkeys() method.
Sendkeys() method works well with both web based as well as windows based applications. You can send a single keystroke or a combination of keys to the active window. The only thing you need to keep in your mind is that your keystrokes would be received by any application, which currently has the focus.
To send keystrokes, you would need to instantiate WScript.Shell.
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
Keystroke | Keyboard 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}”
Many More Articles on HP QuickTest Professional

An expert on R&D, Online Training and Publishing. He is M.Tech. (Honours) and is a part of the STG team since inception.
Hi Yogindernath,
Thats a fantastic article for sendkeys tutorial on QTP. I hope this code will run on all versions of QTP. Keep posting more.
Hi,
First of all I would like to appreciate all the efforts you have put in for this excellent website on Software testing. I am going to ask a very basic question on QTP as I am a newbie in QTP and looking to build my career in Automation.
In the above Vbscript code, you have used words like “Set” and “CreateObject”. Can you please throw some light on what they are and how they can be used.
Please apologize me if I am asking a very basic question.
Steven
Nice article. I tried the tips . It works. Thanx once again
Thanks Saurav for the encouraging words. I will try to post more and more articles on QTP soon.
Hi Steven,
First of all, no question is silly. You are most welcome to ask anything even if its a very basic thing.
Anyways, to answer your query, Both “Set” and “CreateObject” are VBScript specific keywords and not QTP specific. The Set command tells VBScript engine that you intend to use the objKey variable to store an object reference. Thats it. However, the CreateObject() function actually does the real work of creating the object and store the reference of the object in objKey variable.
Hi,
I am going to share a very frustrating issue which you. As far as the HP support goes, I had a very bad experience. Initially, its very difficult to find any information on QTP.
Few days back, I ran into a concurrent license issue in QTP 11 and I called the HP customer support. To my horror, I got this reply ‘Quick Test??� is that one of our products?.’
Can anyone answer my question on QTP please. Its really urgent and gating issue for us.QTP 11.0 does not record “SystemUtil.Run” statement on windows. Can anyone provide the resolution please.
Regards,
Ankur Jain
Hi Ankur,
Its a known issue with QTP 11 that it doesnt record SystemUtil.Run statement.As a workaround, you can manually add this yourself till this issue gets fixed.
Cheerz,
Yogindernath
Hi everyone,
I have few doubts related to the mouse movements in order to retrieve the Tool tip for an Object (Link, Image etc). For example my requirement is if the object is located at (x,y) coordinates, I need to move the mouse to that location, wait for some time and then retrieve the tool tip through QTP.
Can anyone please help?
With Regards
Nitisha
Hi Nitisha,
In order to fetch the tool-tip of an image through QTP, use the below code:
Browser(“Browser”).Page(“Page”).GetROProperty(“alt”)
However in order to fetch the tool-tip of a link through QTP, use the below code:
‘ Place mouse cursor over the link
Browser(“Browser”).Page(“Page”).WebElement(“text:=My Yahoo!”).FireEvent “OnMouseOver”
wait 1
‘ Grab tooltip
ToolTip = Window(“nativeclass:=tooltips_class32”).GetROProperty(“text”)
Let me know in case you have any questions on QTP.