-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhotkey.class.js
More file actions
72 lines (62 loc) · 1.23 KB
/
hotkey.class.js
File metadata and controls
72 lines (62 loc) · 1.23 KB
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
class Hotkey {
constructor(name) {
this.name = name;
this.keys = [];
this.clicks = [];
this.fn = null;
}
_has(x, a) {
var h = false;
a.some(function (v, i) {
//if (v === x) h = i;
return h = v === x;
});
return h;
}
_del(x, a) {
a.some(function (v, i) {
if (x === v) {
a.splice(i, 1);
return true;
}
return false;
});
}
bindKey(x) {
if (!this.keybound(x)) {
this.keys.push(x);
}
return this;
}
unbindKey(x) {
this._del(x, this.keys);
return this;
}
keybound(x) {
return this._has(x, this.keys);
}
bindClick(x) {
if (!this.clickbound(x)) {
this.clicks.push(x);
}
return this;
}
unbindClick(x) {
this._del(x, this.clicks);
return this;
}
clickbound(x) {
return this._has(x, this.clicks);
}
run(x) {
if (typeof x === "function") { // Set the function
this.fn = x;
return this; // Allow inline constructing
} else if (this.fn) { // All clear (x is the event)
this.fn(x);
}
}
toString() {
return `${this.name || "Hotkey"}: Bind{Keys:${JSON.stringify(this.keys)}, Clicks:${JSON.stringify(this.clicks)}}, FN:${this.fn!==null}`;
}
}