Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* jshint esversion:6 */
- /**** Extending your module to work with MMM-KeyBindings *****
- *
- * Use the code below in your module to accept key press
- * events generated by the MMM-KeyBindings module.
- *
- * These is a basic implementation, expand as needed.
- *
- */
- var KeyHandler = Class.extend({
- /*** defaults ***
- *
- * Default Key Binding Configuration, can be overwriten on init
- *
- */
- defaults: {
- keyBindings: {
- // If you want a certain key to give your module "focus" set it here:
- takeFocus: { keyName: "Menu", keyState: "KEY_LONGPRESSED" },
- mode: "MYMODE", // The mode, once you have focus, or "DEFAULT" to respond to all keys
- multiInstance: true,
- map: { NextSlide: "ArrowRight", PrevSlide: "ArrowLeft" }
- }
- },
- /* init()
- * Is called when the module is instantiated.
- */
- init: function(name, config) {
- this.name = name;
- this.config = Object.assign({}, this.defaults, config);
- this.currentMode = "DEFAULT";
- if (typeof this.config.multiInstance === undefined) {
- this.config.multiInstance = true;
- }
- this.reverseMap = {};
- for (var eKey in this.config.map) {
- if (this.config.map.hasOwnProperty(eKey)) {
- this.reverseMap[this.config.map[eKey]] = eKey;
- }
- }
- },
- /*** validate ***
- *
- * Add function below to your moduleName.js
- * Add `if (this.validate(notification, payload)) { return; }`
- * to the first line of module's 'notificationRecieved' function.
- *
- * If your module does not already overridde the function, use the snippet below
- * notificationReceived: function(notification, payload, sender) {
- * if (this.validate(notification, payload)) { return; }
- * },
- *
- */
- validate: function(notification, payload) {
- // Handle KEYPRESS mode change events from the MMM-KeyBindings Module
- if (notification === "KEYPRESS_MODE_CHANGED") {
- this.currentMode = payload;
- return true;
- }
- // Uncomment line below for diagnostics & to confirm keypresses are being recieved
- if (notification === "KEYPRESS" && this.debug) { console.log(payload); }
- // Validate Keypresses
- if (notification === "KEYPRESS" && this.currentMode === this.config.mode) {
- if (this.config.multiInstance && payload.sender !== payload.instance) {
- return false; // Wrong Instance
- }
- if (!(payload.keyName in this.reverseMap)) {
- return false; // Not a key we listen for
- }
- this.validKeyPress(payload);
- return true;
- }
- // Test for focus key pressed and need to take focus:
- if (notification === "KEYPRESS" && ("takeFocus" in this.config)) {
- if (this.currentMode === this.config.mode) {
- return false; // Already have focus.
- }
- if (this.config.multiInstance && payload.sender !== payload.instance) {
- return false; // Wrong Instance
- }
- if (typeof this.config.takeFocus === "object") {
- if (this.config.takeFocus.keyPress !== payload.keyPress ||
- this.config.takeFocus.keyState !== payload.keyState) {
- return false; // Wrong keyName/KeyPress Combo
- }
- } else if (typeof this.config.takeFocus === "string" &&
- payload.keyName !== this.config.takeFocus) {
- return false; // Wrong Key;
- }
- this.focusReceived();
- return true;
- }
- return false;
- },
- /*** focusReceived ***
- *
- * Function is called when a valid take focus key press
- * has been received and is ready for action
- *
- */
- focusReceived: function() {
- console.log(this.name + " HAS FOCUS!");
- this.sendNotification("KEYPRESS_MODE_CHANGED", this.config.mode);
- this.currentMode = this.config.mode;
- this.onFocus();
- },
- /*** releaseFocus ***
- *
- * Call this function when ready to release focus
- *
- * Modify this function to do what you need in your module
- * whenever you're ready to give up focus.
- *
- */
- releaseFocus: function() {
- console.log(this.name + " HAS RELEASED FOCUS!");
- this.sendNotification("KEYPRESS_MODE_CHANGED", "DEFAULT");
- this.currentMode = "DEFAULT";
- this.onFocusReleased();
- },
- /**************** SUBCLASSABLE FUNCTIONS ****************/
- /*** Pass your functions in the KeyHandler definition ***/
- /*** validKeyPress ***
- *
- * Add function below to your moduleName.js
- * Function is called when a valid key press for your module
- * has been received and is ready for action
- * Modify this function to do what you need in your module
- * whenever a valid key is pressed.
- *
- */
- validKeyPress: function(kp) {
- if (kp.keyName === this.keyHandler.map.NextSlide) {
- this.nextSlide();
- } else if (kp.keyName === this.keyHandler.map.PrevSlide) {
- this.previousSlide();
- }
- // You can also use this.keyHandler.reverseMap[kp.KeyName] to get the action
- },
- /*
- * Subclass this method in your KeyHandler definition to do something
- * when focus has been receieved.
- * Modify this function to do what you need in your module
- * whenever focus is received.
- */
- onFocus: function(kp) {
- },
- /*
- * Subclass this method in your KeyHandler definition to do something
- * when focus has been receieved.
- * Modify this function to do what you need in your module
- * whenever focus is released.
- */
- onFocusReleased: function(kp) {
- },
- /*
- * Subclassed to provide reference to module's send function.
- */
- sendNotification: function(notification, payload) {},
- notificationReceived: function(notification, payload, sender) {
- Log.info(this.name + " - received notification: " + notification);
- if (notification === 'MODULE_DOM_CREATED') {
- // Register Key Handler
- if (this.config.keyBindings.enabled && MM.getModules().filter(kb => kb.name === "MMM-KeyBindings").length > 0) {
- // First combine our configs,
- this.keyBindings = Object.assign({}, this.keyBindings, this.config.keyBindings)
- // Then, register the handler definition,
- KeyHandler.register(this.name, {
- sendNotification: (n, p) => { this.sendNotification(n,p); }, // Reference to send notifications
- validKeyPress: (kp) => { this.validKeyPress(kp); }, // Your Key Press Function
- onFocus: () => { this.hasFocus(); }, // Do something when you get focus
- onFocusReleased: () => { this.lostFocus(); } // Do something when focus is lost
- });
- // Finally, create the handler.
- this.keyHandler = KeyHandler.create(this.name, this.keyBindings);
- }
- }
- // For all future notifications, check if it is a key press and if we should worry about it.
- if (this.keyHandler && this.keyHandler.validate(notification, payload)) { return; }
- }
- });
- KeyHandler.definitions = {};
- KeyHandler.create = function(name, config) {
- // Make sure module definition is available.
- if (!KeyHandler.definitions[name]) {
- return;
- }
- var handlerDefinition = KeyHandler.definitions[name];
- var clonedDefinition = cloneObject(handlerDefinition);
- // Note that we clone the definition. Otherwise the objects are shared, which gives problems.
- var KeyHandlerClass = KeyHandler.extend(clonedDefinition);
- return new KeyHandlerClass(name, config);
- };
- KeyHandler.register = function(name, handlerDefinition) {
- KeyHandler.definitions[name] = handlerDefinition;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement