As any PowerBuilder developer will know, there is no way in PowerBuilder to dynamically trigger a key press in another application. Through the use of an operating system level api call this is now possible. This functionality can become extremely useful since it allows PowerBuilder to type in a username and password without the need for user input. PowerBuilder also isn't limited to its own windows. This api call triggers the keyboard event and it will cause a key to be pressed and displayed into whatever window is currently active, even if it is another product's window.
The following chart lists the virtual key values that map to the keyboard. When one of these values is passed to the keybd_event api call the key that is mapped to that value is triggered and will display. The behavior, when done programmatically will be identical as if a user had pressed that key.
Key
|
Ascii
|
Key
|
Ascii
|
Key
|
Ascii
|
Key
|
Ascii
|
L-Button
|
1
|
2
|
50
|
W
|
87
|
F12
|
123
|
R-Button
|
2
|
3
|
51
|
X
|
88
|
F13
|
124
|
Cancel
|
3
|
4
|
52
|
Y
|
89
|
F14
|
125
|
M-Button
|
4
|
5
|
53
|
Z
|
90
|
F15
|
126
|
Back
|
8
|
6
|
54
|
NP - 0
|
96
|
F16
|
127
|
Tab
|
9
|
7
|
55
|
NP - 1
|
97
|
F17
|
128
|
Clear
|
12
|
8
|
56
|
NP - 2
|
98
|
F18
|
129
|
Return
|
13
|
9
|
57
|
NP - 3
|
99
|
F19
|
130
|
Shift
|
16
|
A
|
65
|
NP - 4
|
100
|
F20
|
131
|
Control
|
17
|
B
|
66
|
NP - 5
|
101
|
F21
|
132
|
Menu
|
18
|
C
|
67
|
NP - 6
|
102
|
F22
|
133
|
Pause
|
19
|
D
|
68
|
NP - 7
|
103
|
F23
|
134
|
Cap
|
20
|
E
|
69
|
NP - 8
|
104
|
F24
|
135
|
Escape
|
27
|
F
|
70
|
NP - 9
|
105
|
Numlock
|
144
|
Space
|
32
|
G
|
71
|
*
|
106
|
Scroll
|
145
|
Prior
|
33
|
H
|
72
|
+
|
107
|
Rshift
|
161
|
Next
|
34
|
I
|
73
|
-
|
109
|
L-Ctrl
|
162
|
End
|
35
|
J
|
74
|
.
|
110
|
R-Ctrl
|
163
|
Home
|
36
|
K
|
75
|
/
|
111
|
L-Menu
|
164
|
Left
|
37
|
L
|
76
|
F1
|
112
|
R-Menu
|
165
|
Up
|
38
|
M
|
77
|
F2
|
113
|
=
|
187
|
Right
|
39
|
N
|
78
|
F3
|
114
|
,
|
188
|
Down
|
40
|
O
|
79
|
F4
|
115
|
[
|
189
|
Select
|
41
|
P
|
80
|
F5
|
116
|
.
|
190
|
PrintScrn
|
44
|
Q
|
81
|
F6
|
117
|
/
|
191
|
Insert
|
45
|
R
|
82
|
F7
|
118
|
'
|
192
|
Delete
|
46
|
S
|
83
|
F8
|
119
|
[
|
219
|
Help
|
47
|
T
|
84
|
F9
|
120
|
/
|
220
|
0
|
48
|
U
|
85
|
F10
|
121
|
]
|
221
|
1
|
49
|
V
|
86
|
F11
|
122
|
'
|
222
|
|
Triggering Print Screen Dynamically:
Global or Local External Function Declaration:
SUBROUTINE keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo) LIBRARY "user32.dll"
Powerscript: (Triggering the Print Screen)
keybd_event( 44, 1, 0, 0 ) // Places the entire screen into the clipboard.
keybd_event( 44, 0, 0, 0 ) // Places justthe active window into the clipboard. Equivalent to ALT + Print Scrn.
Powerscript: (This code dynamically triggers a PASTE. Equivalent to pressing CTRL + V)
rte_1.setfocus()
send(handle(rte_1),770,0, 0)
Triggering Other Keys Dynamically:
Global or Local External Function Declaration:
(Declaration is the same in all cases)
SUBROUTINE keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo) LIBRARY "user32.dll"
Powerscript: (This script will trigger the key "A" to be pressed.)
integer li_vkey
li_vkey = 65
sle_1.setfocus() // By having focus on an SLE the key being triggerred will be displayed.
keybd_event( li_vkey, 1, 0, 0 ) // See the above chart for the key value.
Mapping a Function Key to a Key Press:
The following code will allow you to map the F4 key to Shift + Tab and F5 key to Tab. This api call is required in order to correctly trigger the dynamic key press and to properly release the shift key.
Global External Functions:
SUBROUTINE keybd_event (int bVk, int bScan, int dwFlags, int dwExtraInfo) LIBRARY "user32.dll"
Script: (Key event)
Choose Case key
Case KeyF4!
// Back tabbing with F4 key
keybd_event(16,0,0,0) // SHIFT Depressed
keybd_event(9,0,0,0) // TAB Depressed
keybd_event(16,0,2,0) // SHIFT Released (CANNOT BE OMITTED)
keybd_event(9,0,2,0) // TAB Released (Can be omitted)
Case KeyF5!
// Forward tabbing with F5 key
keybd_event(9,0,0,0) // TAB Depressed
keybd_event(9,0,2,0) // TAB Released (Can be omitted)
End Choose