Guest User

MMM-keybindings.js

a guest
Mar 16th, 2020
182
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global document, Module, window, Mousetrap, console */
  2. /* Magic Mirror
  3. * Module: MMM-KeyBindings
  4. *
  5. * By shbatm
  6. * MIT Licensed.
  7. */
  8. /* jshint esversion:6 */
  9. var global = this;
  10.  
  11. Module.register("MMM-KeyBindings", {
  12. defaults: {/*
  13. enabledKeyStates: ["KEY_PRESSED"],
  14. evdev: { enabled: false },
  15. handleKeys: [ 'r', 'l' ],
  16. enableMousetrap: true,
  17. enableKeyboard: false,*/
  18. },
  19.  
  20. // Allow for control on muliple instances
  21. 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",
  22.  
  23. requiresVersion: "2.3.0", // Required version of MagicMirror
  24.  
  25. start: function() {
  26. console.log(this.name + " has started...");
  27.  
  28. // Allow Legacy Config Settings:
  29. if (this.config.evdevKeyMap) {
  30. this.config.keyMap = this.config.evdevKeyMap;
  31. }
  32.  
  33. if (this.config.evdev.enabled) {
  34. this.sendSocketNotification("ENABLE_EVDEV", this.config.evdev);
  35. }
  36.  
  37. this.currentKeyPressMode = "DEFAULT";
  38.  
  39. // Generate a reverse key map
  40. this.reverseKeyMap = {};
  41. for (var eKey in this.config.keyMap) {
  42. if (this.config.keyMap.hasOwnProperty(eKey)) {
  43. this.reverseKeyMap[this.config.keyMap[eKey]] = eKey;
  44. }
  45. }
  46. },
  47.  
  48. getScripts: function() {
  49. return ['keyHandler.js', 'mousetrap.min.js', 'mousetrap-global-bind.min.js'];
  50. },
  51.  
  52. setupMousetrap: function() {
  53. var self = this;
  54. var keys = ['home', 'enter', 'left', 'right', 'up', 'down', 'return', 'playpause', 'nexttrack', 'previoustrack', 'Menu'];
  55. var keyCodes = { 179: 'playpause', 178: 'nexttrack', 177: 'previoustrack', 93: 'Menu' };
  56. var keyMap = { ContextMenu: "Menu" };
  57.  
  58. Mousetrap.addKeycodes(keyCodes);
  59.  
  60. // Add extra keys (must be in Mousetrap form)
  61. // TODO: Add ability to add extra keycodes as well
  62. keys = keys.concat(this.config.handleKeys);
  63.  
  64. // Remove Disabled Keys
  65. for (var i = this.config.disableKeys.length - 1; i >= 0; i--) {
  66. var j = keys.indexOf(this.config.disableKeys[i]);
  67. if (j > -1) {
  68. keys.splice(j, 1);
  69. }
  70. }
  71.  
  72. // console.log(keys);
  73.  
  74. Mousetrap.bindGlobal(keys, (e) => {
  75. // Prevent the default action from occuring
  76. if (e.preventDefault) {
  77. e.preventDefault();
  78. } else {
  79. // internet explorer
  80. e.returnValue = false;
  81. }
  82.  
  83. var payload = {};
  84. payload.keyName = e.key;
  85.  
  86. // Standardize the name
  87. if (payload.keyName in keyMap) {
  88. payload.keyName = keyMap[payload.keyName];
  89. }
  90.  
  91. if (this.config.evdev.rawMode) {
  92. payload.keyState = e.type;
  93. } else {
  94. payload.keyState = "KEY_PRESSED";
  95. }
  96. payload.currentMode = self.currentKeyPressMode;
  97. payload.sender = self.instance;
  98. payload.instance = self.instance;
  99. payload.protocol = "mousetrap";
  100. self.sendNotification("KEYPRESS", payload);
  101. self.doAction(payload);
  102. });
  103.  
  104. // Squash bad actors:
  105. Mousetrap.bind(['home', 'Menu'], (e) => {
  106. e.preventDefault();
  107. return false;
  108. }, 'keyup');
  109. },
  110.  
  111. handleEvDevKeyPressEvents: function(payload) {
  112. // Add the current mode to the payload
  113. payload.currentMode = this.currentKeyPressMode;
  114.  
  115. // Add the sender to the payload (useful if you have multiple clients connected;
  116. // the evdev keys only work on the main server)
  117. payload.sender = "SERVER";
  118. payload.protocol = "evdev";
  119. payload.instance = this.instance;
  120.  
  121. // Standardize the name
  122. if (payload.keyName in this.reverseKeyMap) {
  123. payload.keyName = this.reverseKeyMap[payload.keyName];
  124. }
  125. this.sendNotification("KEYPRESS", payload);
  126. this.doAction(payload);
  127. },
  128.  
  129. // socketNotificationReceived from helper
  130. socketNotificationReceived: function(notification, payload) {
  131. // console.log("Working notification system. Notification:", notification, "payload: ", payload);
  132. if (notification === "KEYPRESS") {
  133. if (this.config.enabledKeyStates.indexOf(payload.keyState) > -1) {
  134. this.handleEvDevKeyPressEvents(payload);
  135. }
  136. }
  137. },
  138.  
  139. notificationReceived: function(notification, payload, sender) {
  140. if (notification === "DOM_OBJECTS_CREATED") {
  141. if (this.config.enableKeyboard) {
  142. console.log("Setting up Mousetrap keybindings.");
  143. this.setupMousetrap();
  144. }
  145. }
  146. if (notification === "KEYPRESS_MODE_CHANGED") {
  147. this.currentKeyPressMode = payload || "DEFAULT";
  148. }
  149. },
  150.  
  151. doAction: function(payload) {
  152. let action = this.config.actions.filter(k => k.key === payload.keyName);
  153. if (action) {
  154. action.forEach(a => {
  155. if (a.state && a.state !== payload.keyState) { return; }
  156. if (a.instance && a.instance !== payload.sender) { return; }
  157. if (a.mode && a.mode !== payload.currentMode) { return; }
  158.  
  159. if ("changeMode" in a) {
  160. this.currentKeyPressMode = a.changeMode;
  161. this.sendNotification("KEYPRESS_MODE_CHANGED", a.changeMode);
  162. } else {
  163. this.sendNotification(a.notification, a.payload);
  164. }
  165. });
  166. }
  167. }
  168. });
RAW Paste Data