-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_keycode.html
93 lines (85 loc) · 3.47 KB
/
test_keycode.html
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
87
88
89
90
91
92
<html>
<head>
<title>Keycode library test</title>
<script src = "keycode.js"></script>
<script type = "text/javascript" src = "jsunit/app/jsUnitCore.js"></script>
</head>
<body>
<h1>Keycode library test</h1>
<div id = "keyPrintout">Current key: </div>
<div id = "codePrintout">Current code: </div>
<div>X is down: <span id = "x_down">false</span></div>
<div>Shift-X is down: <span id = "shift_x_down">false</span></div>
<div>Shift-C is down: <span id = "shift_c_down">false</span></div>
<div>Ctrl-A is down: <span id = "ctrl_a_down">false</span></div>
<div>Raw keycode: <span id = "raw_keycode"></span></div>
<script type = "text/javascript">
// Here be all jsUnit test cases...
var k = KeyCode;
function simple_key(code) { return k.hot_key({ code: code }); };
function test_lower_a() { assertEquals('A', simple_key(k.key('a'))); };
function test_upper_a() { assertEquals('A', simple_key(k.key('A'))); };
function test_z() { assertEquals('Z', simple_key(k.key('z'))); };
function test_f1() { assertEquals('F1', simple_key(k.fkey(1))); };
function test_f12() { assertEquals('F12', simple_key(k.fkey(12))); };
function test_num0() { assertEquals('Num0', simple_key(k.numkey(0))); };
function test_num9() { assertEquals('Num9', simple_key(k.numkey(9))); };
function test_shift() {
assertEquals('Shift+X', k.hot_key({ code: k.key('x'), shift: true }));
};
function test_ctrl_shift() {
assertEquals('Ctrl+Shift+F12', k.hot_key({ code: k.fkey(12),
shift: true, ctrl: true }));
};
</script>
<script type = "text/javascript">
var fields = ['charCode', 'keyCode', 'which', 'type', 'timeStamp',
'altKey', 'ctrlKey', 'shiftKey', 'metaKey'];
var types = ['keydown', 'keypress', 'keyup'];
function show_event(type) {
return function(e) {
var lines = [];
for(var i = 0; i < fields.length; ++i) {
lines.push('<b>' + fields[i] + '</b>: ' + e[fields[i]] + '<br />');
}
document.getElementByI(type).innerHTML = lines.join('\n');
return false;
}
};
function show_is_key_down(id, code, ctrl, alt, shift) {
document.getElementById(id + '_down').innerHTML = KeyCode.is_down({
code: code,
ctrl: ctrl,
alt: alt,
shift: shift
});
};
function update_key_downs() {
show_is_key_down('x', KeyCode.key('x'), false, false, false);
show_is_key_down('shift_x', KeyCode.key('x'), false, false, true);
show_is_key_down('shift_c', KeyCode.key('c'), false, false, true);
show_is_key_down('ctrl_a', KeyCode.key('a'), true, false, false);
};
document.onkeydown = function(e) {
e = e || window.event
var k = KeyCode;
document.getElementById('keyPrintout').innerHTML =
'Current key: ' + k.hot_key(k.translate_event(e));
document.getElementById('codePrintout').innerHTML =
'Current code: ' + k.translate_event(e).code;
document.getElementById('raw_keycode').innerHTML = e.which || e.keyCode;
KeyCode.key_down(e);
if(e.preventDefault) {
e.preventDefault();
}
return false;
};
document.onkeypress = function(e) {
e.preventDefault();
return false;
};
document.onkeyup = KeyCode.key_up;
window.setInterval(update_key_downs, 500);
</script>
</body>
</html>