Sendkeys Method – Keyboard Oprations in QTP

Hello Friends!
In this post we’ll learn how to perform keyboard operation in QTP using vbscript.
What’s the need of Sendkeys?
Hmmm… ok.. Refer the blow code.
Set oShell = CreateObject(“WScript.Shell”)
SystemUtil.Run “D:Program FilesMercury InteractiveQuickTest Professionalsamplesflightappflight4a.exe”
Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Click
oShell.SendKeys “john”
Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Click
oShell.SendKeys “wrong “
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
Copy this code in QTP and run it.
Okie.. Now you can see, because we have entered incorrect password, following error dialog will be displayed.
Now we need to close error dialog and enter correct password in the login window.

If do this using DP code, we need to get the properties of error window and write one more line of code.

Dialog(“text:=Flight Reservations”).WinButton(“text:=OK”).Click

Instead of addind more objects in our code and writing extra lines for this, we can simply press escape key (ESC) from keyboard and our job is done!!

How can we do that using QTP?

All you need to do is, create a Wscript object and then you can use Sendkeys for that purpose.

Set oShell = CreateObject(“WScript.Shell”)
oShell.SendKeys “{ESC}”

Done!!

This thing is very useful when application have no automation interface. And it’s quite fast as well. Depends on the need.

Most keyboard characters are represented by a single keystroke. Some keyboard characters are made up of combinations of keystrokes (CTRL+SHIFT+HOME, for example).

Please note:-

– To send a single keyboard character, send the character itself as the string argument. For example,
– To send the letter x, send the string argument “x”.
– To send a space, send the string ” “.
– You cannot send the PRINT SCREEN key {PRTSC} to an application.

Examples:
  • oShell.SendKeys “{ESC}”    – press escape key
  • oShell.SendKeys “john”    – type john
  • oShell.SendKeys “+^{ESC}”    – press Ctrl + Shift + Esc keys (open Windows Task Manager)
  • oShell.SendKeys “%{F}”    – press Alt + F key (open file menu)

For details of all the characters, please refer the following table.

In case of any queries, please post your comments.

7 thoughts on “Sendkeys Method – Keyboard Oprations in QTP”

  1. Hi… I want a shortcut for print screen key on keyboard…While I am {PRTSC},its asking me for a printer than printing the screen..can anyone help me with this issue..

    Thanks in advance

  2. Hi, I am using below script to print the screen in paint, but not working. Can anyone assist me

    Set oShell = CreateObject("WScript.Shell")
    oShell.SendKeys "+^{ESC}"
    Systemutil.Run "D:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe"
    oShell.SendKeys "^{PRTSC}"
    Systemutil.Run "D:WINDOWSSystem32mspaint.exe"
    wait(5)
    oShell.SendKeys "^Set oShell = CreateObject("WScript.Shell")
    oShell.SendKeys "+^{ESC}"
    Systemutil.Run "D:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe"
    oShell.SendKeys "^{PRTSC}"
    Systemutil.Run "D:WINDOWSSystem32mspaint.exe"
    wait(5)
    oShell.SendKeys "^V"

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top