Project

General

Profile

testKey.html

Sergey Ivanovskiy, 06/14/2022 05:44 AM

Download (6.46 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
var p2j = {};
7
p2j.isFirefox = true;
8
</script>
9

    
10
<script type="text/javascript" src="src/com/goldencode/p2j/ui/client/driver/web/res/p2j.js"></script>
11
<script type="text/javascript" src="src/com/goldencode/p2j/ui/client/driver/web/res/p2j.keymap.js"></script>
12
<script type="text/javascript" src="src/com/goldencode/p2j/ui/client/driver/web/res/p2j.keyboard.js"></script>
13
<script language="JavaScript">
14

15
var lines= 0;
16
var maxlines= 1000;
17

18
function init()
19
{
20
   var cfg = {
21
      isGui : true
22
   };
23
   p2j.keymap.init(cfg);
24
   //
25
   p2j.clipboard = {enabled: false};
26
   p2j.socket = {send: function(data)
27
         {
28
            console.log(data);
29
         },
30

31
         sendKeyCode: function(keyCode, keyState)
32
         {
33
            console.log("keyCode=" + keyCode + " keyState=" + keyState);
34
         },
35
    
36
   };
37
   //p2j.keyboard.init(cfg);
38

39
    document.testform.t.value+= '';
40
    lines= 0;
41

42
    if (document.addEventListener)
43
    {
44
       document.addEventListener("keydown",keydown,false);
45
       document.addEventListener("keypress",keypress,false);
46
       document.addEventListener("keyup",keyup,false);
47
       document.addEventListener("textInput",textinput,false);
48
    }
49
    else if (document.attachEvent)
50
    {
51
       document.attachEvent("onkeydown", keydown);
52
       document.attachEvent("onkeypress", keypress);
53
       document.attachEvent("onkeyup", keyup);
54
       document.attachEvent("ontextInput", textinput);
55
    }
56
    else
57
    {
58
       document.onkeydown= keydown;
59
       document.onkeypress= keypress;
60
       document.onkeyup= keyup;
61
       document.ontextinput= textinput;   // probably doesn't work
62
    }
63
/*
64
    window.addEventListener("beforeunload", 
65
                            function (event)
66
                            {
67
                               // Cancel the event as stated by the standard.
68
                               event.preventDefault();
69
                               // Chrome requires returnValue to be set.
70
                               event.returnValue = '';
71
                            });*/
72
}
73

74
function showmesg(t)
75
{
76
   var old= document.testform.t.value;
77
   if (lines >= maxlines)
78
   {
79
      var i= old.indexOf('\n');
80
   if (i >= 0)
81
       old= old.substr(i+1);
82
   }
83
   else
84
      lines++;
85

86
   document.testform.t.value= old + t + '\n';
87
}
88

89
function keyval(n)
90
{
91
    if (n == null) return 'undefined';
92
    var s= pad(3,n);
93
    if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
94
    while (s.length < 9) s+= ' ';
95
    return s;
96
}
97

98
function keymesg(w,e)
99
{
100
    var row= 0;
101
    var head= [w, '        '];
102
    if (document.testform.classic.checked)
103
    {
104
   showmesg(head[row] +
105
            ' keyCode=' + keyval(e.keyCode) +
106
       ' which=' + keyval(e.which) +
107
            ' charCode=' + keyval(e.charCode) +
108
       ' AltGraph=' + e.getModifierState("AltGraph") );
109
   row= 1;
110
    }
111
    if (document.testform.modifiers.checked)
112
    {
113
   showmesg(head[row] +
114
            ' shiftKey='+pad(5,e.shiftKey) +
115
       ' ctrlKey='+pad(5,e.ctrlKey) +
116
       ' altKey='+pad(5,e.altKey) +
117
       ' metaKey='+pad(5,e.metaKey));
118
   row= 1;
119
    }
120
    if (document.testform.dom3.checked)
121
    {
122
   showmesg(head[row] +
123
       ' key='+e.key +
124
       ' keyValue='+ e.key.codePointAt(0) +
125
       ' char='+e.char +
126
       ' code='+e.code +
127
       ' location='+e.location +
128
       ' repeat='+e.repeat);
129
   row= 1;
130
    }
131
    if (document.testform.olddom3.checked)
132
    {
133
   showmesg(head[row] +
134
       ' keyIdentifier='+ pad(8,e.keyIdentifier)+
135
       ' keyLocation='+e.keyLocation);
136
   row= 1;
137
    }
138
    if (document.testform.p4gl.checked)
139
    {
140
       
141
   showmesg(head[row] +
142
       ' hasLabel='+ pad(8,p2j.keymap.keyboardMapping.hasLabelForCode(p2j.keymap.mapKey(e)))+
143
       ' 4gl code='+p2j.keymap.mapKey(e));
144
   row= 1;
145
    }
146
    if (row == 0)
147
   showmesg(head[row]);
148
}
149

150
function pad(n,s)
151
{
152
   s+= '';
153
   while (s.length < n) s+= ' ';
154
   return s;
155
}
156

157
function suppressdefault(e,flag)
158
{
159
   if (flag)
160
   {
161
       if (e.preventDefault) e.preventDefault();
162
       if (e.stopPropagation) e.stopPropagation();
163
   }
164
   return !flag;
165
}
166

167
function keydown(e)
168
{
169
   if (!e) e= event;
170
   keymesg('keydown ',e);
171
   return suppressdefault(e,document.testform.keydown.checked);
172
}
173

174
function keyup(e)
175
{
176
   if (!e) e= event;
177
   keymesg('keyup   ',e);
178
   return suppressdefault(e,document.testform.keyup.checked);
179
}
180

181
function keypress(e)
182
{
183
   if (!e) e= event;
184
   keymesg('keypress',e);
185
   return suppressdefault(e,document.testform.keypress.checked);
186
}
187

188
function textinput(e)
189
{
190
   if (!e) e= event;
191
   //showmesg('textInput  data=' + e.data);
192
   showmesg('textInput data='+e.data);
193
   return suppressdefault(e,document.testform.textinput.checked);
194
}
195

    
196
</script>
197
</head>
198
<body>
199
<form name="testform">
200
<h2>Javascript Key Event Test Script</h2><p>
201
Type keys in the text area below to see the Javascript events triggered
202
and the values returned.
203
Notes on test results from the page are at
204
<a href="http://unixpapa.com/js/key.html">http://unixpapa.com/js/key.html</a>.
205
<p>
206
On most browsers, suppressing the default action on keypress events prevents
207
the browser from processing the keystroke. On some browsers, suppressing the
208
default action on keydown prevents keypress or keyup events from triggering.
209
Textinput events normally won't be seen unless you turn off default
210
suppression on keydown and keypress.
211
<p>
212
Your browser: <strong>
213
<script>
214
document.write(navigator.userAgent);
215
</script>
216
</strong>
217
<p>
218
<table>
219
<tr><td>Suppress default handling of:</td>
220
<td><input type="checkbox" name="keydown" value="1"> keydown
221
&nbsp;&nbsp;&nbsp;
222
<input type="checkbox" name="keypress" value="1" checked> keypress
223
&nbsp;&nbsp;&nbsp;
224
<input type="checkbox" name="keyup" value="1"> keyup
225
&nbsp;&nbsp;&nbsp;
226
<input type="checkbox" name="textinput" value="1"> textinput
227
</td></tr>
228
<tr><td>Show attribute values for:</td>
229
<td><input type="checkbox" name="classic" value="1" checked> classic
230
&nbsp;&nbsp;&nbsp;
231
<input type="checkbox" name="modifiers" value="1"> modifiers
232
&nbsp;&nbsp;&nbsp;
233
<input type="checkbox" name="dom3" value="1"> DOM 3
234
&nbsp;&nbsp;&nbsp;
235
<input type="checkbox" name="olddom3" value="1"> old DOM 3
236
&nbsp;&nbsp;&nbsp;
237
<input type="checkbox" name="p4gl" value="1"> Progress 4GL
238
</td></tr>
239
</table>
240
<textarea name="t" rows="24" cols="90"></textarea>
241
<br>
242
<input type="reset" onclick="document.testform.t.value='';lines=0;return false">
243
</form>
244
<script language="JavaScript">
245
init();
246
</script>
247
</body>
248
</html>