Return button in onKeyReleased method. Tests updated. Fixes #145

This commit is contained in:
Francisco Hodge
2019-07-14 11:55:03 -04:00
parent 8ba6f879d0
commit d4e9f51182
12 changed files with 53 additions and 50 deletions
+5 -5
View File
@@ -133,6 +133,11 @@ declare module 'simple-keyboard' {
*/
onKeyPress?: (button: string) => any;
/**
* Executes the callback function on key release.
*/
onKeyReleased?: (button: string) => any;
/**
* Executes the callback function on input change. Returns the current input's string.
*/
@@ -162,11 +167,6 @@ declare module 'simple-keyboard' {
* Executes the callback function on input change. Returns the input object with all defined inputs.
*/
onChangeAll?: (inputs: any) => any;
/**
* Executes the callback function on key release.
*/
onKeyReleased?: () => void;
}
class Keyboard {
+12 -15
View File
@@ -295,15 +295,15 @@ class SimpleKeyboard {
/**
* Handles button mouseup
*/
handleButtonMouseUp() {
handleButtonMouseUp(button) {
this.isMouseHold = false;
if (this.holdInteractionTimeout) clearTimeout(this.holdInteractionTimeout);
/**
* Calling onKeyReleased
*/
if (typeof this.options.onKeyReleased === "function")
this.options.onKeyReleased();
if (button && typeof this.options.onKeyReleased === "function")
this.options.onKeyReleased(button);
}
/**
@@ -672,7 +672,7 @@ class SimpleKeyboard {
}
/**
* Called by {@link caretEventHandler} when an event that warrants a cursor position update is triggered
* Called by {@link setEventListeners} when an event that warrants a cursor position update is triggered
*/
caretEventHandler(event) {
let targetTagName;
@@ -1164,10 +1164,8 @@ class SimpleKeyboard {
this.handleButtonClicked(button);
this.handleButtonMouseDown(button, e);
};
buttonDOM.onpointerup = e => {
this.handleButtonMouseUp();
};
buttonDOM.onpointercancel = e => this.handleButtonMouseUp();
buttonDOM.onpointerup = () => this.handleButtonMouseUp(button);
buttonDOM.onpointercancel = () => this.handleButtonMouseUp(button);
} else {
/**
* Fallback for browsers not supporting PointerEvents
@@ -1180,8 +1178,8 @@ class SimpleKeyboard {
this.handleButtonClicked(button);
this.handleButtonMouseDown(button, e);
};
buttonDOM.ontouchend = e => this.handleButtonMouseUp();
buttonDOM.ontouchcancel = e => this.handleButtonMouseUp();
buttonDOM.ontouchend = () => this.handleButtonMouseUp(button);
buttonDOM.ontouchcancel = () => this.handleButtonMouseUp(button);
} else {
/**
* Handle mouse events
@@ -1190,9 +1188,8 @@ class SimpleKeyboard {
this.isMouseHold = false;
this.handleButtonClicked(button);
};
buttonDOM.onmousedown = e => {
this.handleButtonMouseDown(button, e);
};
buttonDOM.onmousedown = e => this.handleButtonMouseDown(button, e);
buttonDOM.onmouseup = () => this.handleButtonMouseUp(button);
}
}
@@ -1274,8 +1271,8 @@ class SimpleKeyboard {
/**
* Handling ontouchend, ontouchcancel
*/
document.ontouchend = e => this.handleButtonMouseUp();
document.ontouchcancel = e => this.handleButtonMouseUp();
document.ontouchend = () => this.handleButtonMouseUp();
document.ontouchcancel = () => this.handleButtonMouseUp();
} else if (!useTouchEvents) {
/**
* Handling mouseup
+4 -1
View File
@@ -1281,10 +1281,12 @@ it('Keyboard onKeyReleased will work', () => {
let pressed = false;
let firedTimes = 0;
let buttonPressed;
let keyboard = new Keyboard({
onKeyReleased: () => {
onKeyReleased: button => {
pressed = true;
buttonPressed = button;
firedTimes++;
},
debug: true
@@ -1295,4 +1297,5 @@ it('Keyboard onKeyReleased will work', () => {
expect(pressed).toBeTruthy();
expect(firedTimes).toBe(1);
expect(buttonPressed).toBe("q");
});