Project

General

Profile

test.html

Sergey Ivanovskiy, 09/24/2015 10:22 AM

Download (11.7 KB)

 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
<html>
3
<head>
4
<title>Javascript Key Event Tester</title>
5
<script type="text/javascript">
6
"use strict";
7

8
   function simulateKeyPress(elementId, code, ctrlKey, altKey, shiftKey, metaKey) {
9
      simulateKeyPressExt(elementId, code, code, ctrlKey, altKey, shiftKey, metaKey);
10
   }
11

12
   function simulateKeyPressExt(elementId, code, charCode, ctrlKey, altKey, shiftKey, metaKey) {
13
      var ch;
14
      try
15
      {
16
         ch = String.fromCodePoint(charCode);
17
      }
18
      catch (ex)
19
      {
20
         ch = "";
21
      }
22

23
      var input = document.getElementById(elementId);
24

25
      if (ctrlKey)
26
      {
27
         input.dispatchEvent(createKeyDownUpEvent(true, 17, ctrlKey, altKey, shiftKey, metaKey));
28
      }
29
      if (altKey)
30
      {
31
         input.dispatchEvent(createKeyDownUpEvent(true, 18, ctrlKey, altKey, shiftKey, metaKey));
32
      }
33
      if (shiftKey)
34
      {
35
         input.dispatchEvent(createKeyDownUpEvent(true, 16, ctrlKey, altKey, shiftKey, metaKey));
36
      }
37
      var keyDownEvent = createKeyDownUpEvent(true, code, ctrlKey, altKey, shiftKey, metaKey);
38
      input.dispatchEvent(keyDownEvent);
39
      if (keyDownEvent.defaultPrevented)
40
      {
41
         return;
42
      }
43
      if (ch !== "") {
44
         input.dispatchEvent(createKeyPressEvent(ch.charCodeAt(0), ctrlKey, altKey, shiftKey, metaKey));
45
      }
46
      input.dispatchEvent(createKeyDownUpEvent(false, code, ctrlKey, altKey, shiftKey, metaKey));
47
      
48
      if (ctrlKey)
49
      {
50
         input.dispatchEvent(createKeyDownUpEvent(false, 17, false, altKey, shiftKey, metaKey));
51
      }
52
      if (altKey)
53
      {
54
         input.dispatchEvent(createKeyDownUpEvent(false, 18, false, false, shiftKey, metaKey));
55
      }
56
      if (shiftKey)
57
      {
58
         input.dispatchEvent(createKeyDownUpEvent(false, 16, false, false, false, metaKey));
59
      }
60

61
   }
62

63
   function createKeyDownUpEvent(down, code, ctrlKey, altKey, shiftKey, metaKey)
64
   {
65
      var event = document.createEvent("KeyEvents");
66
      
67
      event.initKeyEvent(down ? "keydown" : "keyup", false, true, null, ctrlKey,
68
            altKey, shiftKey, metaKey, code, 0);
69
      return event;
70
   }
71
   
72
   function createKeyPressEvent(charCode, ctrlKey, altKey, shiftKey, metaKey)
73
   {
74
      var event = document.createEvent("KeyEvents");
75
      
76
      event.initKeyEvent("keypress", false, true, null, ctrlKey,
77
            altKey, shiftKey, metaKey, 0, charCode);
78
      return event;
79
   }
80
</script>
81
<script type="text/javascript" >
82
var p2j = {};
83
p2j.isFirefox = true;
84
</script>
85

    
86
<script type="text/javascript" src=p2j.keymap.js></script>
87
<script type="text/javascript" src=p2j.keyboard.js></script>
88
<script language="JavaScript">
89

90
function init(isGui)
91
{
92
   var cfg = {
93
      isGui : isGui
94
   };
95
   p2j.keyboard.init(cfg);
96
   p2j.clipboard = {
97
         enabled : false,
98
         needsSelection : function() {},
99
         haveSelection  : function() {},
100
         clearSelection : function() {}
101
   };
102
   p2j.socket = {
103
         send : function(data)
104
         {
105
            var ch = data[1] << 8 | data[2];
106
            addResult(document.testform.results, ch);
107
         }};
108
   
109
   document.testform.testArea.value = '';
110
   document.testform.results.value = '';
111
}
112

113
function addResult(el, t)
114
{
115
   var old= el.value;
116
   el.value = old + '\n' + t;
117
}
118

119
function run4GLKeyTest(isGui)
120
{
121
   init(isGui);
122
//   0     CTRL-@
123
   simulateKeyPress("testArea", '2'.codePointAt(0), true, false, false, false);
124
//   1     CTRL-A
125
   simulateKeyPress("testArea", 'A'.codePointAt(0), true, false, false, false);
126
//   2     CTRL-B
127
   simulateKeyPress("testArea", 'B'.codePointAt(0), true, false, false, false);
128
//   3     CTRL-C
129
   simulateKeyPress("testArea", 'C'.codePointAt(0), true, false, false, false);
130
//   4     CTRL-D
131
   simulateKeyPress("testArea", 'D'.codePointAt(0), true, false, false, false);
132
//   5     CTRL-E
133
   simulateKeyPress("testArea", 'E'.codePointAt(0), true, false, false, false);
134
//   6     CTRL-F
135
   simulateKeyPress("testArea", 'F'.codePointAt(0), true, false, false, false);
136
//   7     CTRL-G                           BELL
137
   simulateKeyPress("testArea", 'G'.codePointAt(0), true, false, false, false);
138
//   8     BACKSPACE                        BACKSPACE
139
   simulateKeyPress("testArea", 8, false, false, false, false);
140
//   9     TAB                              TAB
141
   simulateKeyPress("testArea", 9, false, false, false, false);
142
//  10     CTRL-J
143
   simulateKeyPress("testArea", 'J'.codePointAt(0), true, false, false, false);
144
//  11     CTRL-K
145
   simulateKeyPress("testArea", 'K'.codePointAt(0), true, false, false, false);
146
//  12     CTRL-L
147
   simulateKeyPress("testArea", 'L'.codePointAt(0), true, false, false, false);
148
//  13     ENTER                            RETURN
149
   simulateKeyPress("testArea", 13, false, false, false, false);
150
// 14     CTRL-N
151
// 15     CTRL-O
152
// 16     CTRL-P
153
// 17     CTRL-Q
154
// 18     CTRL-R
155
// 19     CTRL-S
156
// 20     CTRL-T
157
// 21     CTRL-U
158
// 22     CTRL-V
159
// 23     CTRL-W
160
// 24     CTRL-X
161
// 25     CTRL-Y
162
// 26     CTRL-Z
163
   for (var code = 78; code <= 90; code++)
164
   {
165
      simulateKeyPress("testArea", code, true, false, false, false);
166
   }
167

168
//  27     ESC                              END-ERROR
169
//because of CHUI it needs to call ESC two times to clear internal esc state.
170
   simulateKeyPress("testArea", 27, false, false, false, false);
171
   simulateKeyPress("testArea", 27, false, false, false, false);
172
//  28     CTRL-'\'
173
   simulateKeyPressExt("testArea", 220, 92, true, false, false, false);
174
//  29     CTRL-]
175
   simulateKeyPressExt("testArea", 221, 93, true, false, false, false);
176
//  30     CTRL-^
177
   simulateKeyPress("testArea", '6'.codePointAt(0), true, false, false, false);
178
//  31     CTRL-_
179
   simulateKeyPressExt("testArea", 173, 45, true, false, false, false);
180
   
181
//   32     space
182
//   33     !                                !
183
//   34     "                                "
184
//   35     #                                #
185
//   36     $                                $
186
//   37     %                                %
187
//   38     &                                &
188
//   39     '                                '
189
//   40     (                                (
190
//   41     )                                )
191
//   42     *                                *
192
//   43     +                                +
193
//   44     ,                                ,
194
//   45     -                                -
195
//   46     .                                .
196
//   47     /                                /
197
//   48     0                                0
198
//   49     1                                1
199
//   50     2                                2
200
//   51     3                                3
201
//   52     4                                4
202
//   53     5                                5
203
//   54     6                                6
204
//   55     7                                7
205
//   56     8                                8
206
//   57     9                                9
207
//   58     :                                :
208
//   59     ;                                ;
209
//   60     <                                <
210
//   61     =                                =
211
//   62     >                                >
212
//   63     ?                                ?
213
//   64     @                                @
214
//   65     A                                A
215
//   66     B                                B
216
//   67     C                                C
217
//   68     D                                D
218
//   69     E                                E
219
//   70     F                                F
220
//   71     G                                G
221
//   72     H                                H
222
//   73     I                                I
223
//   74     J                                J
224
//   75     K                                K
225
//   76     L                                L
226
//   77     M                                M
227
//   78     N                                N
228
//   79     O                                O
229
//   80     P                                P
230
//   81     Q                                Q
231
//   82     R                                R
232
//   83     S                                S
233
//   84     T                                T
234
//   85     U                                U
235
//   86     V                                V
236
//   87     W                                W
237
//   88     X                                X
238
//   89     Y                                Y
239
//   90     Z                                Z
240
//   91     [                                [
241
//   92     \                                \
242
//   93     ]                                ]
243
//   94     ^                                ^
244
//   95     _                                _
245
//   96     `                                `
246
//   97     a                                a
247
//   98     b                                b
248
//   99     c                                c
249
//   100     d                                d
250
//   101     e                                e
251
//   102     f                                f
252
//   103     g                                g
253
//   104     h                                h
254
//   105     i                                i
255
//   106     j                                j
256
//   107     k                                k
257
//   108     l                                l
258
//   109     m                                m
259
//   110     n                                n
260
//   111     o                                o
261
//   112     p                                p
262
//   113     q                                q
263
//   114     r                                r
264
//   115     s                                s
265
//   116     t                                t
266
//   117     u                                u
267
//   118     v                                v
268
//   119     w                                w
269
//   120     x                                x
270
//   121     y                                y
271
//   122     z                                z
272
//   123     {                                {
273
//   124     |                                |
274
//   125     }                                }
275
//   126     ~                                ~
276
//   127     DEL                              DELETE-CHARACTER
277

278

279
//   500
280
//   501     CURSOR-UP                        CURSOR-UP
281
//   502     CURSOR-DOWN                      CURSOR-DOWN
282
//   503     CURSOR-RIGHT                     CURSOR-RIGHT
283
//   504     CURSOR-LEFT                      CURSOR-LEFT
284
//   505     HOME                             HOME
285
//   506     END                              END
286
//   507     PAGE-UP                          PAGE-UP
287
//   508     PAGE-DOWN                        PAGE-DOWN
288
//   509     SHIFT-TAB                        BACK-TAB
289
     simulateKeyPress("testArea", 9, false, false, true, false);
290

291
//   510     INS                              INSERT-MODE
292
//   511     HELP-KEY
293
//   512     DELETE
294
//   513     EXECUTE
295
//   514     PAGE
296
//   515     FIND
297
//   516     INS-LINE
298
//   517     DEL-LINE
299
//   518     LINE-ERASE
300
//   519     PAGE-ERASE
301
//   520     SHIFT-BACKSPACE
302
//   521     SHIFT-TAB
303
//   522     EXIT
304
//   523
305
//   524
306
//   525     SHIFT-ENTER
307
//   526
308
//   527
309
//   528
310
//   529
311
//   530
312
//   531
313
//   532
314
//   533
315
//   534
316
//   535     ERASE
317
//   536     WHITE
318
//   537     BLUE
319
//   538     RED
320
//   539     SHIFT-ESC
321
//   540
322
//   541     CTRL-BREAK                       STOP
323
//   542
324
//   543
325
//   544
326
//   545
327
//   546
328
//   547
329
//   548
330
//   549
331

332
}
333
</script>
334
</head>
335
<body>
336
<form name="testform">
337
<input type="button" value="Run CHUI Test" onclick="void(run4GLKeyTest(false));"/>
338
<input type="button" value="Run GUI Test" onclick="void(run4GLKeyTest(true));"/>
339
<textarea id="testArea" name="testArea" rows="25" cols="90" style="display:block;"></textarea>
340
<textarea id="results" name="results" rows="4" cols="90"></textarea>
341
<br>
342
</form>
343
</body>
344
</html>