Mercury Device Replay and Scan Codes. What is that?

Datetime:2016-08-23 01:14:42          Topic:          Share

What does Device Replay and Scan Codes have to do with UFT/QTP? In Nov 2007 I wrote an article on 4 Ways to send keyboard input to an application using QTP when it does not record using normal recording The four ways discussed in detail in the article were:

  1. Using Type method
  2. Using SendKeys method
  3. Using Low level/analog recording
  4. Using Mercury.DeviceReplay method

#1 and #2 are used frequently by people however people sparingly use #3 and #4.

They rarely use #3 because it is dependent on mouse coordinates and #4 largely because DeviceReplay remains an undocumented method from Mercury (now HP) even after so many years.

However, we still get questions on this method on QTP/UFT forums and our social media channels like Twitter and Facebook. Here is a recent tweet

@LearnQTP

1 – can you help me?I can’t make the keyboard button PAUSE work.

What happnes? i’m used this code

— Anderson Vieira (@AndersonVVale) July 11, 2016

@LearnQTP

wait 2

Set myReplay = creatobject (“Mercury.DeviceReplay”)

myReplay.PressKey 116 ‘PAUSE

wait 2

— Anderson Vieira (@AndersonVVale) July 11, 2016

We can use Mercury.DeviceReplay to send keyboard input on Windows operating system. The challenge arise when we have to find out what value to use for PressKey method.

In this article we will delve into what are the codes or values that can be used for sending keyboard inputs and how they are arrived. Most UFT engineers tend to think that these values are derived from ASCII value. That’s a fallacy. Rather, these values are a decimal equivalent of something known as IBM Scan Codes.

What are Scan Codes?

A scancode (or scan code ) is the data that most computer keyboards send to a computer to report which keys have been pressed. A number, or sequence of numbers, is assigned to each key on the keyboard. Source

To understand let us first see the image below.

Each key on a standard keyboard is assigned a number as shown in the keyboard layout above. If you have ever noticed, while the positioning for PrtScr/Insert/Pause/Break/PgUp/PgDn/Home/End keys change from keyboard to keyboard , the positioning for function keys, QWERTY keys, numeric keys remain consistent across all modern day laptops and desktops.

How are Scan Codes related to DeviceReplay method?

All keys on your keyboard are mapped to Scan codes in hexadecimal which are sent to the Windows operating system when we press any key.

PressKey method discussed above use these hexadecimal numbers in decimal format. Here is a complete table mapping Keys on Keyboard, Hexadecimal Scan Code and Decimal Code. You would need  Decimal Codes when you need to use PressKey method on Mercury.DeviceReplay object.

KEY Scan Code (Hex) Decimal Code ( For UFT)
A 1E 30
B 30 48
C 2E 46
D 20 32
E 12 18
F 21 33
G 22 34
H 23 35
I 17 23
J 24 36
K 25 37
L 26 38
M 32 50
N 31 49
O 18 24
P 19 25
Q 10 16
R 13 19
S 1F 31
T 14 20
U 16 22
V 2F 47
W 11 17
X 2D 45
Y 15 21
Z 2C 44
0 0B 11
1 2 2
2 3 3
3 4 4
4 5 5
5 6 6
6 7 7
7 8 8
8 9 9
9 0A 10
` 29 41
- 0C 12
= 0D 13
\ 2B 43
BKSP 0E 14
SPACE 39 57
TAB 0F 15
CAPS 3A 58
L SHFT 2A 42
L CTRL 1D 29
L ALT 38 56
R SHFT 36 54
R CTRL E0,1D 157
R ALT E0,38 184
ENTER 1C 28
ESC 1 1
F1 3B 59
F2 3C 60
F3 3D 61
F4 3E 62
F5 3F 63
F6 40 64
F7 41 65
F8 42 66
F9 43 67
F10 44 68
F11 57 87
F12 58 88
PRNT  SCRN E0,2A,  E0,37
SCROLL 46 70
PAUSE E1,1D,45, E1,9D,C5
[ 1A 26
INSERT E0,52 210
HOME E0,47 199
PG UP E0,49 201
DELETE E0,53 211
END E0,4F 207
PG DN E0,51 209
U ARROW E0,48 200
L ARROW E0,4B 203
D ARROW E0,50 208
R ARROW E0,4D 205
NUM 45 69
KP / E0,35 181
KP * 37 55
KP - 4A 74
KP + 4E 78
KP EN E0,1C 156
KP . 53 83
KP 0 52 82
KP 1 4F 79
KP 2 50 80
KP 3 51 81
KP 4 4B 75
KP 5 4C 76
KP 6 4D 77
KP 7 47 71
KP 8 48 72
KP 9 49 73
] 1B 27
; 27 39
' 28 40
, 33 51
. 34 52
/ 35 53

KP stands for K ey P ad keys which are available on the right side of all keyboards.

The above table has been derived from this Microsoft document . We have stripped down several terms mentioned in the document to explain the concept for UFT engineers in as much simple terms as possible.

Example: Press F1 using DeviceReplay Method

Set oDevRp = CreateObject ("Mercury.DeviceReplay") oDevRp.PressKey 59 'Press F1

Did this article help you? In case you have any questions, please comment below.