-
Notifications
You must be signed in to change notification settings - Fork 1
/
keybd-key-pad.htm
86 lines (73 loc) · 2.92 KB
/
keybd-key-pad.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!-- http://www.javascripter.net/faq/keyboardinputfiltering.htm -->
<form action=# name=f2 id=f2 onsubmit="return false">
<table>
<tr><td>Non Numpad</td>
<td>
<input type=text name=t1 id=t1f2 value="" size=25 style="width:300px;">
</td>
<tr><td>Numpad Only</td>
<td>
<input type=text name=t2 id=t2f2 value="" size=25 style="width:300px;">
</td>
</tr>
</table>
</form>
<script type="text/javascript" language=JavaScript>
function inNumpad(e, atNumpad) {
var chrTyped, chrCode=0, evt=e?e:event;
if (evt.charCode!=null) chrCode = evt.charCode;
else if (evt.which!=null) chrCode = evt.which;
else if (evt.keyCode!=null) chrCode = evt.keyCode;
if (chrCode==0) chrTyped = 'SPECIAL KEY';
else chrTyped = String.fromCharCode(chrCode);
console.log(`chrCode=${chrCode}| [chrTyped=${chrTyped}]: match=(${evt.code.match(/Numpad[a-zA-Z0-9]*|[\b]|SPECIAL/)?true:false}) evt=`,evt)
//Digits, special keys & backspace [\b] work as usual:
if (evt.code.match(/Numpad[a-zA-Z0-9|[\b]|SPECIAL/)) return atNumpad;
// if (chrTyped.match(/Numpad[a-zA-Z0-9|[\b]|SPECIAL/)) return atNumpad;
// if (evt.altKey || evt.ctrlKey || chrCode<28) return atNumpad;
//Any other input? Prevent the default response:
if (evt.preventDefault) evt.preventDefault();
evt.returnValue=!atNumpad;
return !atNumpad;
}
function inputNumpadOnly(e) {
return inNumpad(e, true);
}
function inputNonNumpadOnly(e) {
return inNumpad(e, false);
}
function inputDigitsOnly(e) {
var chrTyped, chrCode=0, evt=e?e:event;
if (evt.charCode!=null) chrCode = evt.charCode;
else if (evt.which!=null) chrCode = evt.which;
else if (evt.keyCode!=null) chrCode = evt.keyCode;
console.log(`chrCode=${chrCode}| evt=`,evt)
if (chrCode==0) chrTyped = 'SPECIAL KEY';
else chrTyped = String.fromCharCode(chrCode);
//[test only:] display chrTyped on the status bar
self.status='inputDigitsOnly: chrTyped = '+chrTyped;
//Digits, special keys & backspace [\b] work as usual:
if (chrTyped.match(/\d|[\b]|SPECIAL/)) return true;
if (evt.altKey || evt.ctrlKey || chrCode<28) return true;
//Any other input? Prevent the default response:
if (evt.preventDefault) evt.preventDefault();
evt.returnValue=false;
return false;
}
function addEventHandler(elem,eventType,handler) {
if (elem.addEventListener)
elem.addEventListener (eventType,handler,false);
else if (elem.attachEvent)
elem.attachEvent ('on'+eventType,handler);
else
return 0;
return 1;
}
// onload: Call the init() function to add event handlers!
function init() {
addEventHandler(self.document.f2.elements[0],'keypress',inputNonNumpadOnly);
addEventHandler(self.document.f2.elements[1],'keypress',inputNumpadOnly);
document.title='Ready!'
}
setTimeout(init, 100);
</script>