-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
60 lines (43 loc) · 1.09 KB
/
index.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
var keynameOf = require("keyname-of");
var events = require("dom-event");
module.exports = on;
module.exports.on = on;
module.exports.off = off;
function on (element, keys, callback) {
var expected = parse(keys);
var fn = events.on(element, 'keyup', function(event){
if ((event.ctrlKey || undefined) == expected.ctrl &&
(event.altKey || undefined) == expected.alt &&
(event.shiftKey || undefined) == expected.shift &&
keynameOf(event.keyCode) == expected.key){
callback(event);
}
});
callback['cb-' + keys] = fn;
return callback;
}
function off (element, keys, callback) {
events.off(element, 'keyup', callback['cb-' + keys]);
}
function parse (keys){
var result = {};
keys = keys.split(/[^\w]+/);
var i = keys.length, name;
while ( i -- ){
name = keys[i].trim();
if(name == 'ctrl') {
result.ctrl = true;
continue;
}
if(name == 'alt') {
result.alt = true;
continue;
}
if(name == 'shift') {
result.shift = true;
continue;
}
result.key = name.trim();
}
return result;
}