From 9abe0d191fc0edfe236e9a6dbefc5bf7a3c2b80e Mon Sep 17 00:00:00 2001 From: James Rowe Date: Tue, 3 Dec 2019 16:55:51 +0000 Subject: [PATCH] bindKey with inputOptions for sendInput Add a new parameter to the bindKey method to specify inputOptions. I have a case where more than one piece of information was needed in a call to sendInput, which is easy to achieve by directly calling it in an event handler but to bind that to a keypress meant manually repeating lots of the code already in KeyboardControls class. --- src/controls/KeyboardControls.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/controls/KeyboardControls.js b/src/controls/KeyboardControls.js index 0a3942e..8c50264 100644 --- a/src/controls/KeyboardControls.js +++ b/src/controls/KeyboardControls.js @@ -199,8 +199,18 @@ class KeyboardControls { // handle repeat press if (this.boundKeys[keyName].options.repeat || this.keyState[keyName].count == 0) { + + // callback to get live parameters if function + let parameters = this.boundKeys[keyName].parameters; + if (typeof parameters === "function") { + parameters = parameters(); + } + // todo movement is probably redundant - this.clientEngine.sendInput(this.boundKeys[keyName].actionName, { movement: true }); + let inputOptions = Object.assign({ + movement: true + }, parameters || {}); + this.clientEngine.sendInput(this.boundKeys[keyName].actionName, inputOptions); this.keyState[keyName].count++; } } @@ -226,8 +236,10 @@ class KeyboardControls { * @param {String} actionName - the event name * @param {Object} options - options object * @param {Boolean} options.repeat - if set to true, an event continues to be sent on each game step, while the key is pressed + * @param {Object/Function} parameters - parameters (or function to get parameters) to be sent to + * the server with sendInput as the inputOptions */ - bindKey(keys, actionName, options) { + bindKey(keys, actionName, options, parameters) { if (!Array.isArray(keys)) keys = [keys]; let keyOptions = Object.assign({ @@ -235,7 +247,7 @@ class KeyboardControls { }, options); keys.forEach(keyName => { - this.boundKeys[keyName] = { actionName, options: keyOptions }; + this.boundKeys[keyName] = { actionName, options: keyOptions, parameters: parameters }; }); }