-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MMM-KeyBindings.js
executable file
·240 lines (214 loc) · 6.22 KB
/
MMM-KeyBindings.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* global Mousetrap */
/* MagicMirror²
* Module: MMM-KeyBindings
*
* By shbatm
* MIT Licensed.
*/
/* jshint esversion:6 */
var global = this;
Module.register("MMM-KeyBindings", {
defaults: {
enabledKeyStates: ["KEY_PRESSED", "KEY_LONGPRESSED"], // Other options are 'KEY_UP', 'KEY_DOWN',
// 'KEY_HOLD' but evdev.raw_mode must be true to receive
handleKeys: [], // List of additional keys to handle in this module; blank == standard set
disableKeys: [], // list of keys to ignore from the default set.
enableKeyboard: false,
evdev: {
enabled: true,
eventPath: "/dev/input/btremote"
},
keyMap: {
Home: "KEY_HOMEPAGE",
Enter: "KEY_KPENTER",
ArrowLeft: "KEY_LEFT",
ArrowRight: "KEY_RIGHT",
ArrowUp: "KEY_UP",
ArrowDown: "KEY_DOWN",
Menu: "KEY_MENU",
MediaPlayPause: "KEY_PLAYPAUSE",
MediaNextTrack: "KEY_FASTFORWARD",
MediaPreviousTrack: "KEY_REWIND",
Return: "KEY_BACK"
},
actions: [
{
key: "Home",
state: "KEY_LONGPRESSED",
instance: "SERVER",
mode: "DEFAULT",
notification: "REMOTE_ACTION",
payload: { action: "MONITORTOGGLE" }
// Can also be:
// changeMode: "NEW_MODE"
// instead of notification & payload
}
]
},
// Allow for control on muliple instances
instance:
global.location &&
[
"localhost",
"127.0.0.1",
"::1",
"::ffff:127.0.0.1",
undefined,
"0.0.0.0"
].indexOf(global.location.hostname) > -1
? "SERVER"
: "LOCAL",
requiresVersion: "2.3.0", // Required version of MagicMirror
start: function () {
Log.info(`${this.name} has started…`);
// Allow Legacy Config Settings:
if (this.config.evdevKeyMap) {
this.config.keyMap = this.config.evdevKeyMap;
}
if (this.config.evdev.enabled) {
this.sendSocketNotification("ENABLE_EVDEV", this.config.evdev);
}
this.currentKeyPressMode = "DEFAULT";
// Generate a reverse key map
this.reverseKeyMap = {};
for (var eKey in this.config.keyMap) {
if (this.config.keyMap.hasOwnProperty(eKey)) {
this.reverseKeyMap[this.config.keyMap[eKey]] = eKey;
}
}
},
getScripts: function () {
return [
"keyHandler.js",
this.file("node_modules/mousetrap/mousetrap.min.js"),
this.file("node_modules/mousetrap-global-bind/mousetrap-global-bind.min.js")
];
},
setupMousetrap: function () {
var self = this;
var keys = [
"home",
"enter",
"left",
"right",
"up",
"down",
"return",
"playpause",
"nexttrack",
"previoustrack",
"Menu"
];
var keyCodes = {
179: "playpause",
178: "nexttrack",
177: "previoustrack",
93: "Menu"
};
var keyMap = { ContextMenu: "Menu" };
Mousetrap.addKeycodes(keyCodes);
// Add extra keys (must be in Mousetrap form)
// TODO: Add ability to add extra keycodes as well
keys = keys.concat(this.config.handleKeys);
// Remove Disabled Keys
for (var i = this.config.disableKeys.length - 1; i >= 0; i--) {
var j = keys.indexOf(this.config.disableKeys[i]);
if (j > -1) {
keys.splice(j, 1);
}
}
// Log.log(keys);
Mousetrap.bindGlobal(keys, (e) => {
// Prevent the default action from occuring
if (e.preventDefault) {
e.preventDefault();
} else {
// internet explorer
e.returnValue = false;
}
var payload = {};
payload.keyName = e.key;
// Standardize the name
if (payload.keyName in keyMap) {
payload.keyName = keyMap[payload.keyName];
}
if (this.config.evdev.rawMode) {
payload.keyState = e.type;
} else {
payload.keyState = "KEY_PRESSED";
}
payload.currentMode = self.currentKeyPressMode;
payload.sender = self.instance;
payload.instance = self.instance;
payload.protocol = "mousetrap";
self.sendNotification("KEYPRESS", payload);
self.doAction(payload);
});
// Squash bad actors:
Mousetrap.bind(
["home", "Menu"],
(e) => {
e.preventDefault();
return false;
},
"keyup"
);
},
handleEvDevKeyPressEvents: function (payload) {
// Add the current mode to the payload
payload.currentMode = this.currentKeyPressMode;
// Add the sender to the payload (useful if you have multiple clients connected;
// the evdev keys only work on the main server)
payload.sender = "SERVER";
payload.protocol = "evdev";
payload.instance = this.instance;
// Standardize the name
if (payload.keyName in this.reverseKeyMap) {
payload.keyName = this.reverseKeyMap[payload.keyName];
}
this.sendNotification("KEYPRESS", payload);
this.doAction(payload);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
// Log.log("Working notification system. Notification:", notification, "payload: ", payload);
if (notification === "KEYPRESS") {
if (this.config.enabledKeyStates.indexOf(payload.keyState) > -1) {
this.handleEvDevKeyPressEvents(payload);
}
}
},
notificationReceived: function (notification, payload, sender) {
if (notification === "DOM_OBJECTS_CREATED") {
if (this.config.enableKeyboard) {
Log.log("Setting up Mousetrap keybindings.");
this.setupMousetrap();
}
}
if (notification === "KEYPRESS_MODE_CHANGED") {
this.currentKeyPressMode = payload || "DEFAULT";
}
},
doAction: function (payload) {
let action = this.config.actions.filter((k) => k.key === payload.keyName);
if (action) {
action.forEach((a) => {
if (a.state && a.state !== payload.keyState) {
return;
}
if (a.instance && a.instance !== payload.sender) {
return;
}
if (a.mode && a.mode !== payload.currentMode) {
return;
}
if ("changeMode" in a) {
this.currentKeyPressMode = a.changeMode;
this.sendNotification("KEYPRESS_MODE_CHANGED", a.changeMode);
} else {
this.sendNotification(a.notification, a.payload);
}
});
}
}
});