QTP Secret Code Chart Revealed For DeviceReplay PressKey, KeyDown, KeyUp and PressNKeys
Why this post?
Let’s face it. If you’ve been doing test automation for any length of time you’ve faced situations in which you need to perform mouse or keyboard actions using screen coordinates.
When the Set and Type Methods Suck:
Even though most objects have Set and Type methods, these methods -- for whatever reason -- may not work in all situations. If they don’t then your next step may be to try VBSCript’s sendkey method. But even sendkeys sometimes do not have a way to perform certain actions, such as pressing the left alt key. Hence the need for QTP’s undocumented Device Replay features.
No ASCII No Problem:
A few months back, I posted an article outlining three different ways to use keyboard input in QTP. 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.
Chart of the keyboard key codes for the PressKey, Keydown, Keyup and PressNKeys:
The number keys:
The keypad number keys:
The Function keys:
The Letter keys:
The Enter,Space,Shift,Ctrl and Alt keys:
The Weird/Other keys:
Examples:
'Device Replay Example using the calculator with the multiple key
Set myReplay = Createobject("Mercury. DeviceReplay")
SystemUtil.Run "calc.exe"
Window("regexpwndtitle:= Calculator").Activate
myReplay.PressKey 73'9
wait 1
myReplay.PressKey 55' Keypad Multiply
wait 1
myReplay.PressKey 81'3
wait 1
myReplay.PressKey 156'Enter
The end result should be 27
'Device Replay Example using notepad with the shift key
Set myReplay = Createobject("Mercury. DeviceReplay")
SystemUtil.Run "notepad.exe"
wait 1
Window("nativeclass:=Notepad") .Activate
wait 1
myReplay.KeyDown 42''Hold shift key down
wait 1
myReplay.PressKey 36'T
wait 1
myReplay.PressKey 24'H
wait 1
myReplay.PressKey 18'I
wait 2
myReplay.KeyUp 42'Press shift key up
wait 1
myReplay.PressKey 57 'space
wait 1
myReplay.PressKey 36' t
wait 1
myReplay.PressKey 24 'h
wait 1
myReplay.PressKey 18 ie
The example should type out: THIthi
Why this post?
Let’s face it. If you’ve been doing test automation for any length of time you’ve faced situations in which you need to perform mouse or keyboard actions using screen coordinates.
When the Set and Type Methods Suck:
Even though most objects have Set and Type methods, these methods -- for whatever reason -- may not work in all situations. If they don’t then your next step may be to try VBSCript’s sendkey method. But even sendkeys sometimes do not have a way to perform certain actions, such as pressing the left alt key. Hence the need for QTP’s undocumented Device Replay features.
No ASCII No Problem:
A few months back, I posted an article outlining three different ways to use keyboard input in QTP. 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.
Chart of the keyboard key codes for the PressKey, Keydown, Keyup and PressNKeys:
The number keys:
Keystroke | Code to use |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
0 | 0 |
Keystroke | Code to use |
1 | 79 |
2 | 80 |
3 | 81 |
4 | 75 |
5 | 76 |
6 | 77 |
7 | 71 |
8 | 72 |
9 | 73 |
0 | 82 |
Keystroke | Code to use |
F1 | 59 |
F2 | 60 |
F3 | 61 |
F4 | 62 |
F5 | 63 |
F6 | 64 |
F7 | 65 |
F8 | 66 |
F9 | 67 |
F10 | 68 |
F11 | 87 |
F12 | 88 |
Keystroke | Code to use |
A | 30 |
B | 48 |
C | 46 |
D | 32 |
E | 18 |
F | 33 |
G | 34 |
H | 35 |
I | 23 |
J | 36 |
K | 37 |
L | 38 |
M | 50 |
N | 49 |
O | 24 |
P | 25 |
Q | 16 |
R | 19 |
S | 31 |
T | 20 |
U | 22 |
V | 47 |
W | 17 |
X | 45 |
Y | 21 |
Z | 44 |
Keystroke | Code to use |
ENTER | 28 |
SPACE | 57 |
LEFT SHIFT | 42 |
LEFT CTRL | 29 |
LEFT ALT | 56 |
RIGHT SHIFT | 54 |
RIGHT CTRL | 157 |
RIGHT ALT | 184 |
; | 39 |
' | 40 |
, | 51 |
. | 52 |
/ | 53 |
Keystroke | Code to use |
- | 12 |
[ | 26 |
] | 27 |
` | 41 |
= | 13 |
BACKSPACE | 14 |
CAPSLOCK | 58 |
DELETE | 211 |
DOWN | 208 |
END | 207 |
HOME | 199 |
INSERT | 210 |
LEFT | 203 |
NUMLOCK | 69 |
KEYPAD DECIMAL | 83 |
KEYPAD DIVIDE | 181 |
KEYPAD ENTER | 156 |
KEYPAD MINUS | 74 |
KEYPAD MULTIPLY | 55 |
PAUSE | 116 |
PAGE DOWN | 209 |
PAGE UP | 201 |
RIGHT | 205 |
SCROLL LOCK | 70 |
TAB | 15 |
UP | 20O |
'Device Replay Example using the calculator with the multiple key
Set myReplay = Createobject("Mercury.
SystemUtil.Run "calc.exe"
Window("regexpwndtitle:=
myReplay.PressKey 73'9
wait 1
myReplay.PressKey 55' Keypad Multiply
wait 1
myReplay.PressKey 81'3
wait 1
myReplay.PressKey 156'Enter
The end result should be 27
'Device Replay Example using notepad with the shift key
Set myReplay = Createobject("Mercury.
SystemUtil.Run "notepad.exe"
wait 1
Window("nativeclass:=Notepad")
wait 1
myReplay.KeyDown 42''Hold shift key down
wait 1
myReplay.PressKey 36'T
wait 1
myReplay.PressKey 24'H
wait 1
myReplay.PressKey 18'I
wait 2
myReplay.KeyUp 42'Press shift key up
wait 1
myReplay.PressKey 57 'space
wait 1
myReplay.PressKey 36' t
wait 1
myReplay.PressKey 24 'h
wait 1
myReplay.PressKey 18 ie
The example should type out: THIthi
If you are going to repost someone else's original content, please credit the original poster. I'm sure Joe would appreciate it. I'm glad your link under the "No ASCII..." section still points to his site, at least.
ReplyDeletehttps://www.joecolantonio.com/2011/07/19/qtp-secret-code-chart-revealed-for-devicereplay-presskey-keydown-keyup-and-pressnkeys/