This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binds.js
executable file
·149 lines (130 loc) · 3.1 KB
/
binds.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
147
148
149
// Variable for shortcuts
const shortcuts = {
active: [],
}
// Variable for currently held keys
// Maybe I should change global.keys to something else.
// It kinda sounds weird, considering there's nothing else under global.
// The reason I had it originally is mildly complicated.
var global = {
keys: {
other: "",
Alt: false,
Meta: false,
Shift: false,
Control: false,
}
}
// Make new shortcut
function registerShortcut(shortcut, action, prefs) {
def = {
toLowerCase: true,
pressType: "up"
};
if (prefs != undefined) {
// Unreadable and arguably "bad" code
// Refactor later!
let objPrefs = Object.keys(prefs);
let objDef = Object.keys(def);
for (let i = 0; i < objPrefs.length; i++) {
for (let ii = 0; ii < objDef.length; ii++) {
if (objPrefs[i] == objDef[ii]) {
if (eval(`prefs.${objPrefs[i]}`) != eval(`def.${objDef[i]}`) && eval(`prefs.${objPrefs[i]}`) != undefined) {
eval(`def.${objDef[ii]} = prefs.${objPrefs[i]}`)
}
}
}
}
}
let keys = shortcut.split("+");
for (let i = 0; i < keys.length; i++) {
switch(keys[i].toLowerCase()) {
case "ctrl":
case "control":
keys[i] = "Control";
break;
case "cmd":
case "mod4":
case "meta":
case "super":
case "command":
case "windows":
keys[i] = "Meta";
break;
case "shift":
keys[i] = "shift";
break;
case "alt":
case "option":
keys[i] = "alt";
break;
default:
if (def.toLowerCase) { keys[i] = keys[i].toLowerCase() }
}
}
let length = shortcuts.active.length;
shortcuts.active[length] = {
shortcut: keys,
action: action,
type: "up",
}
}
// Change keys in global.active
function setHeld(key, state) {
let modifier = false;
switch(key) {
case "Control":
case "Shift":
case "Meta":
case "Alt":
modifier = true;
break;
}
if (!modifier) {
if (state) {
global.keys.other += key;
} else {
global.keys.other = global.keys.other.replace(new RegExp(key, "g"), "")
}
} else {
if (eval(`global.keys.${key}`) != undefined) {
eval(`global.keys.${key} = ${state}`)
}
}
}
// Compares active keys and all registered shortcuts to see if any are supposed to run
function checkShortcut() {
for (let i = 0; i < shortcuts.active.length; i++) {
let passed = true;
for (let ii = 0; ii < shortcuts.active[i].shortcut.length; ii++) {
let key = shortcuts.active[i].shortcut[ii]
switch(key) {
case "Control":
if (global.keys.Control == false) {passed = false;continue;}
break;
case "Shift":
if (global.keys.Shift == false) {passed = false;continue;}
break;
case "Meta":
if (global.keys.Meta == false) {passed = false;continue;}
break;
case "Alt":
if (global.keys.Alt == false) {passed = false;continue;}
break;
default:
if (global.keys.other.includes(key) != true) {passed = false;}
}
}
if (passed) {
eval(shortcuts.active[i].action)
}
}
}
document.body.addEventListener("keydown", (e) => {
setHeld(e.key, true)
})
document.body.addEventListener("keyup", (e) => {
checkShortcut()
setHeld(e.key, false)
})
//registerShortcut("Ctrl-Y","console.log('stuff')")