3 ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay

QTP/UFT Published on:
UFT UPdate

There are three ways to use keyboard input in QuickTest Professional and Unified Functional Testing: Type, SendKeys and Device Replay. Why, you might ask, use the Type or Sendkeys method at all?  While you wouldn’t want to automate a whole script using SendKeys only, sometimes you may, on occasion, need to automate a section of an application where there is no object recognition.

For example, some unsupported grids or tree views in Quick Test Pro can be navigated using a combination of the HOME, END and arrow keys.  Also — I’ve seen applications where some fields need to have a keyboard event occur to trigger some other behavior (like tabbing of a field) using the objects standard Set method –- which would not work in this case.

1. QTP’s TYPE METHOD

Most objects support the TYPE method.  Type will enter the specified string into an object or perform a certain keyboard combination against an application.  For example:

To tab off an object you would use the following syntax:

SwfObject(“swfname:=Blank”).Type micTab

To enter text:
SwfObject(“swfname:=Blank”).Type “This is my string”

To send an enter keystroke:

SwfObject(“swfname:=Blank”).Type “ “

You can also send a combination of keystrokes at one time. The following holds down the CTRL and the Shift Keys, then presses the “L” key and releases the CTRL and Shift keys:
SwfObject("swfname:=Blank").Type micCtrlDwn + micShiftDwn + "L" + micShiftUp + micCtrlUp

*Important to remember: If you send a down keystroke, be sure to follow it with its corresponding up stroke, as seen in the above example.  Problems can arise by sending a micCtrlDwn and forgetting to release it using micCtrlUp. So, rule of thumb — if you are pressing a key, make sure to also release it.

Also Occasionally you may have issues with event not being  triggered when using one of these methods. In that case check out my post of how to use the FireEvent method.

2. VBScript SendKeys Method ( Also check out: QTP's VBscript SendKeys FAQ)

There are instances in which QTP’s Type method does not trigger certain events, or is unable to mimic certain keystrokes.  In these cases, VBScript SendKeys method can be used. To use the SendKeys you need to first set the WshShell object:
Dim mySendKeys
set mySendKeys = CreateObject("WScript.shell")
mySendKeys.SendKeys(“{TAB}”)

To send the text you would use:
mySendKeys.SendKeys(“This is my string”)

To send an enter keystroke:

mySendKeys.SendKeys(“~”)

*A few important tips: Unlike the QTP TYPE method, you will need to use curly braces for arguments like a TAB or Up arrow key strokes. Also — SendKeys has the following special characters:  Alt(%), Ctrl(^), Shift(+) and Enter(~) keys.

So, to send a CTRL and press the A key you would send:
mySendKeys.SendKeys("^A")

If you need to perform the same keystroke multiple times, you can create a compound string argument. This will allow you to perform a specific keystroke and repeat it any number of times. To select, say, the 10th row in a grid control you might use:

mySendKeys.SendKeys(“{DOWN 10}”)

This will send the down key ten times. (For a more detailed explanation of SendKeys check out VBScript Programmer's Reference.)

Important – You’ll need to make sure that the application or object you wish to receive the keystroke has focus before sending the keystroke.

*Common issues with this method:

(Sometimes multiple keystrokes will not work. If this is the case, try executing each one in a separate line.)

3. Device Replay

This is an undocumented and unsupported QuickTest method, but can be used as a last resort. To employ this method, you’ll need to create a Device Replay object.(Check out the Device Replay Chart of Codes)

To tab off an object:
Dim myDeviceReplay
Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")
myDeviceReplay.PressKey 15

*Remember that Device Replay uses ASCII characters
To send text you would use:

myDeviceReplay.SendString “This is my string”

To send an enter keystroke:

myDeviceReplay.PressKey 28 ‘ASCII code for enter

From The HP knowledge base:

The 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.

Since I posted this I recently came across some more Device Replay Key codes for the PressKey, Keydown, Keyup and PressNKeys that I’d like to share with you. These methods are particularly helpful in certain situations in which you need to perform an action using a code that does not have an ASCII equivalent. Check out my QTP Secret Code Chart Revealed for more info: https://testguild.com/2011/07/19/qtp-secret-code-chart-revealed-for-devicereplay-presskey-keydown-keyup-and-pressnkeys/

Hope this helps.

UFT UPdate