Guest User

keyhandler.js

a guest
Mar 11th, 2020
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* jshint esversion:6 */
  2.  
  3. /**** Extending your module to work with MMM-KeyBindings *****
  4. *
  5. * Use the code below in your module to accept key press
  6. * events generated by the MMM-KeyBindings module.
  7. *
  8. * These is a basic implementation, expand as needed.
  9. *
  10. */
  11. var KeyHandler = Class.extend({
  12. /*** defaults ***
  13. *
  14. * Default Key Binding Configuration, can be overwriten on init
  15. *
  16. */
  17. defaults: {
  18. keyBindings: {
  19. // If you want a certain key to give your module "focus" set it here:
  20. takeFocus: { keyName: "Menu", keyState: "KEY_LONGPRESSED" },
  21. mode: "MYMODE", // The mode, once you have focus, or "DEFAULT" to respond to all keys
  22. multiInstance: true,
  23. map: { NextSlide: "ArrowRight", PrevSlide: "ArrowLeft" }
  24. }
  25. },
  26.  
  27. /* init()
  28. * Is called when the module is instantiated.
  29. */
  30. init: function(name, config) {
  31. this.name = name;
  32. this.config = Object.assign({}, this.defaults, config);
  33.  
  34. this.currentMode = "DEFAULT";
  35. if (typeof this.config.multiInstance === undefined) {
  36. this.config.multiInstance = true;
  37. }
  38. this.reverseMap = {};
  39. for (var eKey in this.config.map) {
  40. if (this.config.map.hasOwnProperty(eKey)) {
  41. this.reverseMap[this.config.map[eKey]] = eKey;
  42. }
  43. }
  44. },
  45.  
  46. /*** validate ***
  47. *
  48. * Add function below to your moduleName.js
  49. * Add `if (this.validate(notification, payload)) { return; }`
  50. * to the first line of module's 'notificationRecieved' function.
  51. *
  52. * If your module does not already overridde the function, use the snippet below
  53. * notificationReceived: function(notification, payload, sender) {
  54. * if (this.validate(notification, payload)) { return; }
  55. * },
  56. *
  57. */
  58. validate: function(notification, payload) {
  59. // Handle KEYPRESS mode change events from the MMM-KeyBindings Module
  60. if (notification === "KEYPRESS_MODE_CHANGED") {
  61. this.currentMode = payload;
  62. return true;
  63. }
  64.  
  65. // Uncomment line below for diagnostics & to confirm keypresses are being recieved
  66. if (notification === "KEYPRESS" && this.debug) { console.log(payload); }
  67.  
  68. // Validate Keypresses
  69. if (notification === "KEYPRESS" && this.currentMode === this.config.mode) {
  70. if (this.config.multiInstance && payload.sender !== payload.instance) {
  71. return false; // Wrong Instance
  72. }
  73. if (!(payload.keyName in this.reverseMap)) {
  74. return false; // Not a key we listen for
  75. }
  76. this.validKeyPress(payload);
  77. return true;
  78. }
  79.  
  80. // Test for focus key pressed and need to take focus:
  81. if (notification === "KEYPRESS" && ("takeFocus" in this.config)) {
  82. if (this.currentMode === this.config.mode) {
  83. return false; // Already have focus.
  84. }
  85. if (this.config.multiInstance && payload.sender !== payload.instance) {
  86. return false; // Wrong Instance
  87. }
  88. if (typeof this.config.takeFocus === "object") {
  89. if (this.config.takeFocus.keyPress !== payload.keyPress ||
  90. this.config.takeFocus.keyState !== payload.keyState) {
  91. return false; // Wrong keyName/KeyPress Combo
  92. }
  93. } else if (typeof this.config.takeFocus === "string" &&
  94. payload.keyName !== this.config.takeFocus) {
  95. return false; // Wrong Key;
  96. }
  97.  
  98. this.focusReceived();
  99. return true;
  100. }
  101.  
  102. return false;
  103. },
  104.  
  105. /*** focusReceived ***
  106. *
  107. * Function is called when a valid take focus key press
  108. * has been received and is ready for action
  109. *
  110. */
  111. focusReceived: function() {
  112. console.log(this.name + " HAS FOCUS!");
  113. this.sendNotification("KEYPRESS_MODE_CHANGED", this.config.mode);
  114. this.currentMode = this.config.mode;
  115. this.onFocus();
  116. },
  117.  
  118. /*** releaseFocus ***
  119. *
  120. * Call this function when ready to release focus
  121. *
  122. * Modify this function to do what you need in your module
  123. * whenever you're ready to give up focus.
  124. *
  125. */
  126. releaseFocus: function() {
  127. console.log(this.name + " HAS RELEASED FOCUS!");
  128. this.sendNotification("KEYPRESS_MODE_CHANGED", "DEFAULT");
  129. this.currentMode = "DEFAULT";
  130. this.onFocusReleased();
  131. },
  132.  
  133.  
  134.  
  135. /**************** SUBCLASSABLE FUNCTIONS ****************/
  136. /*** Pass your functions in the KeyHandler definition ***/
  137.  
  138. /*** validKeyPress ***
  139. *
  140. * Add function below to your moduleName.js
  141. * Function is called when a valid key press for your module
  142. * has been received and is ready for action
  143. * Modify this function to do what you need in your module
  144. * whenever a valid key is pressed.
  145. *
  146. */
  147. validKeyPress: function(kp) {
  148. if (kp.keyName === this.keyHandler.map.NextSlide) {
  149. this.nextSlide();
  150. } else if (kp.keyName === this.keyHandler.map.PrevSlide) {
  151. this.previousSlide();
  152. }
  153. // You can also use this.keyHandler.reverseMap[kp.KeyName] to get the action
  154. },
  155. /*
  156. * Subclass this method in your KeyHandler definition to do something
  157. * when focus has been receieved.
  158. * Modify this function to do what you need in your module
  159. * whenever focus is received.
  160. */
  161. onFocus: function(kp) {
  162.  
  163. },
  164. /*
  165. * Subclass this method in your KeyHandler definition to do something
  166. * when focus has been receieved.
  167. * Modify this function to do what you need in your module
  168. * whenever focus is released.
  169. */
  170. onFocusReleased: function(kp) {
  171.  
  172. },
  173.  
  174. /*
  175. * Subclassed to provide reference to module's send function.
  176. */
  177. sendNotification: function(notification, payload) {},
  178.  
  179. notificationReceived: function(notification, payload, sender) {
  180. Log.info(this.name + " - received notification: " + notification);
  181. if (notification === 'MODULE_DOM_CREATED') {
  182. // Register Key Handler
  183. if (this.config.keyBindings.enabled && MM.getModules().filter(kb => kb.name === "MMM-KeyBindings").length > 0) {
  184. // First combine our configs,
  185. this.keyBindings = Object.assign({}, this.keyBindings, this.config.keyBindings)
  186. // Then, register the handler definition,
  187. KeyHandler.register(this.name, {
  188. sendNotification: (n, p) => { this.sendNotification(n,p); }, // Reference to send notifications
  189. validKeyPress: (kp) => { this.validKeyPress(kp); }, // Your Key Press Function
  190. onFocus: () => { this.hasFocus(); }, // Do something when you get focus
  191. onFocusReleased: () => { this.lostFocus(); } // Do something when focus is lost
  192. });
  193. // Finally, create the handler.
  194. this.keyHandler = KeyHandler.create(this.name, this.keyBindings);
  195. }
  196. }
  197.  
  198. // For all future notifications, check if it is a key press and if we should worry about it.
  199. if (this.keyHandler && this.keyHandler.validate(notification, payload)) { return; }
  200. }
  201.  
  202. });
  203.  
  204. KeyHandler.definitions = {};
  205.  
  206. KeyHandler.create = function(name, config) {
  207.  
  208. // Make sure module definition is available.
  209. if (!KeyHandler.definitions[name]) {
  210. return;
  211. }
  212.  
  213. var handlerDefinition = KeyHandler.definitions[name];
  214. var clonedDefinition = cloneObject(handlerDefinition);
  215.  
  216. // Note that we clone the definition. Otherwise the objects are shared, which gives problems.
  217. var KeyHandlerClass = KeyHandler.extend(clonedDefinition);
  218.  
  219. return new KeyHandlerClass(name, config);
  220.  
  221. };
  222.  
  223. KeyHandler.register = function(name, handlerDefinition) {
  224. KeyHandler.definitions[name] = handlerDefinition;
  225. };
RAW Paste Data