Skip to content

Commit

Permalink
bindKey with inputOptions for sendInput
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooki authored and namel committed Jan 11, 2020
1 parent 3e82cd5 commit 9abe0d1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/controls/KeyboardControls.js
Expand Up @@ -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++;
}
}
Expand All @@ -226,16 +236,18 @@ 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({
repeat: false
}, options);

keys.forEach(keyName => {
this.boundKeys[keyName] = { actionName, options: keyOptions };
this.boundKeys[keyName] = { actionName, options: keyOptions, parameters: parameters };
});
}

Expand Down

0 comments on commit 9abe0d1

Please sign in to comment.