-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmajic-keys.js
146 lines (125 loc) · 3.78 KB
/
majic-keys.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
var MajicKeys = function() {
var MK = this;
MK.connections = {};
MK.downs = {};
MK.ups = {};
MK.keys = {};
MK.destroy = function() {
window.removeEventListener('keydown', MK.handleKeyDown);
window.removeEventListener('keyup', MK.handleKeyUp);
};
MK.connect = function() {
var alen = arguments.length;
if (alen % 2 == 1) { --alen; }
for (var i=0; i < alen; i += 2) {
var key = arguments[i];
var fn = arguments[i+1];
MK.connections[key] = fn;
}
};
// utility function used by .actions() method just below.
var mkhandler = function(action) {
return function(e) {
MK.actionTracker[action] = true;
};
};
MK.actions = function(actions) {
Object.keys(actions).forEach(function(action) {
var k = actions[action];
if (!(k instanceof Array))
k = [k];
k.forEach(function(a) {
MK.connect(a, mkhandler(action));
});
});
};
MK.onDown = function() {
var alen = arguments.length;
if (alen % 2 == 1) { --alen; }
for (var i=0; i < alen; i += 2) {
var key = arguments[i];
var fn = arguments[i+1];
MK.downs[key] = fn;
}
};
MK.onUp = function() {
var alen = arguments.length;
if (alen % 2 == 1) { --alen; }
for (var i=0; i < alen; i += 2) {
var key = arguments[i];
var fn = arguments[i+1];
MK.ups[key] = fn;
}
};
MK.handleKeyDown = function(e) {
var keys = MK.getKeys(e);
for (var i = 0; i < keys.length; ++i) {
var k = keys[i]
MK.keys[k] = 1;
if (MK.downs[k] !== undefined) MK.downs[k](e);
}
MK.maybeContinue(keys, e);
};
MK.handleKeyUp = function(e) {
var keys = MK.getKeys(e);
for (var i = 0; i < keys.length; ++i) {
var k = keys[i]
delete MK.keys[k]
if (MK.ups[k] !== undefined) MK.ups[k](e);
}
MK.maybeContinue(keys, e);
};
MK.maybeContinue = function(key, e) {
if (!(e.altKey || e.ctrlKey || e.metaKey)) {
e.preventDefault();
}
};
MK.getKeys = function(e) {
// TODO: This needs lots of testing to handle various key event
// paradigms.
var key;
if (e.key !== undefined) {
key = e.key;
}
else if (e.keyIdentifier !== undefined) {
key = e.keyIdentifier;
if (key.substr(0,4) == "U+00" && key.length == 6) {
key = String.fromCharCode(("0x" + key.substr(4,2)) - 0);
}
}
var keys = [];
if (key == ' ' || key == 'Space') {
keys.push(' ');
keys.push('Space');
}
else if (key == 'Tab' || key == '\t') {
keys.push('Tab');
keys.push('\t');
}
else if (key == 'Up' || key == 'Down' || key == 'Left'
|| key == 'Right') {
keys.push('Arrow' + key);
}
else if (key.length == 1 && key.toLowerCase() != key.toUpperCase()) {
keys.push(key.toLowerCase());
keys.push(key.toUpperCase());
}
else {
keys.push(key);
}
return keys;
};
MK.pulse = function(e) {
MK.actionTracker = {};
for (var key in MK.keys) {
var conn = MK.connections[key];
if (conn !== undefined && conn !== null) {
conn(e);
}
}
return MK.actionTracker;
};
window.addEventListener('keydown', MK.handleKeyDown);
window.addEventListener('keyup', MK.handleKeyUp);
};