Tuesday 7 May 2013

SendKeys and DeviceReplay Methods

SendKeys Method

SendKeys method are used to perform a keyboard operation. There are various instances when you require to use them e.g. sometimes objects perform actions when certain key is being pressed like ALT+SHIFT or ALT+RETURN or ENTER or TAB keys.

How to use the Windows Scripting SendKeys method-

1. Create a WScript.Shell object.
2. Activate the browser/Window in which you want to press the keys.
3. Use the SendKeys method to type the key combination.

Example:

'This code Activates the window, type the ID and Password and then click on Enter button-
Set WshShell  = CreateObject ("WScript.Shell")
WshShell.AppActivate("Google")
wait(2)

'Input's the user ID and press TAB key
WshShell.SendKeys(userId)
WshShell.SendKeys("{TAB}")

'Inputs the password and press TAB key
WshShell.SendKeys(password)
WshShell.SendKeys("{TAB}")

'Press the Enter Key
WshShell.SendKeys("~")
'Releasing the object
Set WshShell  =Nothing

Keys along with Codes that can be used using SendKeys method-

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.


We use SendKeys method to send keyboard input/keystrokes to applications that do not have automation interface. We can also send more than one keystroke at a time using Sendkeys method. To send keystrokes x,y and z, you would send the string argument "xyz".

Note: You cannot send Print Screen key to an application.


DeviceReplay Method

The Device Replay feature is used to perform mouse and keyboard actions against screen coordinates that are provided. The Device Replay functions are not automatically recorded, but must be programmed manually in the Expert View. 

1. Create the Device Replay Object.
Set DeviceReplay = CreateObject(“Mercury.DeviceReplay”)

2. Call the desired Device Replay function.
DeviceReplay.SendString(“Hello, World”)

3. When done with the Device Replay object, release it.
Set DeviceReplay = Nothing

The Mouse functions that can be used with the Device Replay object are (all coordinates are relative to the top left corner of the screen):


Function Description
MouseMove x, y Move the mouse to the screen coordinate (x,y).
MouseClick x, y, button Move the mouse to the screen coordinate (x,y) and click the button
(0=left; 1=middle; 2=right).
MouseDblClick x, y, button Move the mouse to the screen coordinate (x,y) and double-click the button
(0=left; 1=middle; 2=right).
DragAndDrop x, y, dropx, dropy, button Drag the mouse from screen coordinate (x,y) to (dropx,dropy) with the button
(0=left; 1=middle; 2=right) pressed.
PressKey key Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
MouseDown x, y, button Press the mouse button on screen coordinate (x,y).
MouseUp x, y, button Release the mouse button on screen coordinate (x,y).
KeyDown key Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
KeyUp key Release a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
SendString string Type a string


Mercury DeviceReplay is a powerful utility to simulate keyboard input and also for simulating mouse clicks and movements. At times, Object.Set and Object.Type does not help in QTP for some applications. You can use Mercury DeviceReplay to type Non English symbols and letters even without changing the keyboard layout or installing the special fonts.

Example-

     'Creating Wscript Shell and Device Replay objects

Set WshShell = CreateObject("WScript.Shell")
Set obj = CreateObject("mercury.devicereplay")

x=Page("Page").WebEdit("xyz").GetROProperty ("abs_x")
y=Page("Page").WebEdit("xyz").GetROProperty ("abs_y")

'Clicking Left Mouse button
obj.MouseClick x, y, 1   

'Pressing Enter key
WshShell.sendKeys "{ENTER}"

'Releasing the objects
Set obj=Nothing
Set WshShell=Nothing


Difference Between Mercury DeviceReplay and Sendkeys

Difference between Mercury DeviceReplay and Sendkeys is-

SendKeys -> Support only Keyboard Operations
DeviceReplay-> Support Keyboard + Mouse Operations like Drag Drop too.

In addition to that DeviceReplay supports sending multilingual characters and symbols, while sendkeys support limited keyboard operations.

The other operational difference is dependency on QTP software.

Mercury.DeviceReplay :- It comes with QTP as a module, so you need QTP on the system and only from QTP action you can use it.
SendKeys :- It could be used in VBS script to simulate keyboard inputs with native windows shell commands. It has no dependency on QTP as such. 




1 comment:

  1. Life is short. Time is fast. No replay. No rewind. So enjoy every moment as it comes. See the link below for more info.


    #replay
    www.ufgop.org



    ReplyDelete