Thursday, February 10, 2011

How to use sendkey in QTP script


Definition:
Sends one or more keystrokes to the active window (as if typed on the keyboard).

Syntax:
object.SendKeys(string)

Special character

plus sign       "+",
caret             "^",
percent sign "%",
and tilde       "~"

Send these characters by enclosing them within braces "{}".
For example, to send the plus sign, send the string argument "{+}". Brackets "[ ]" have no special meaning
when used with SendKeys, but you must enclose them within braces to accommodate applications that
do give them a special meaning (for dynamic data exchange (DDE) for example).

To send bracket characters, send the string argument "{[}" for the left bracket and "{]}" for the right one.
To send brace characters, send the string argument "{{}" for the left brace and "{}}" for the right one.
Some keystrokes do not generate characters (such as ENTER and TAB).
Some keystrokes represent actions (such as BACKSPACE and BREAK).
To send these kinds of keystrokes, send the arguments shown in the following table:


Key                                 Argument
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}

Example



Const iNormalFocus = 1
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "mmc.exe",iNormalFocus

Wscript.Sleep 300

objShell.AppActivate "Console1"
Wscript.Sleep 100
objShell.SendKeys "^m"
Wscript.Sleep 100
objShell.SendKeys "{TAB}"
Wscript.Sleep 100
objShell.SendKeys "{TAB}"
Wscript.Sleep 100
objShell.SendKeys "{ENTER}"



2 comments:

rahul said...

How to send windows button key?

Neerajkumar said...

Hi Rahul,

use this "{Ctrl+Esc}"