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

View File

@ -1,6 +1,6 @@
/*!
*
* simple-keyboard v2.24.0
* simple-keyboard v2.24.1
* https://github.com/hodgef/simple-keyboard
*
* Copyright (c) Francisco Hodge (https://github.com/hodgef)

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
/*!
*
* simple-keyboard v2.24.0 (Non-minified build)
* simple-keyboard v2.24.1 (Non-minified build)
* https://github.com/hodgef/simple-keyboard
*
* Copyright (c) Francisco Hodge (https://github.com/hodgef)

10
build/index.d.ts vendored
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 {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
/*!
*
* simple-keyboard v2.24.0 (Non-minified build)
* simple-keyboard v2.24.1 (Non-minified build)
* https://github.com/hodgef/simple-keyboard
*
* Copyright (c) Francisco Hodge (https://github.com/hodgef)
@ -995,15 +995,15 @@
* Handles button mouseup
*/ }, {
key: "handleButtonMouseUp",
value: function handleButtonMouseUp() {
value: function 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);
}
}
/**
@ -1328,7 +1328,7 @@
this.caretEventHandler(event);
}
/**
* 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
*/ }, {
key: "caretEventHandler",
value: function caretEventHandler(event) {
@ -1684,11 +1684,11 @@
_this10.handleButtonClicked(button);
_this10.handleButtonMouseDown(button, e);
};
buttonDOM.onpointerup = function(e) {
_this10.handleButtonMouseUp();
buttonDOM.onpointerup = function() {
return _this10.handleButtonMouseUp(button);
};
buttonDOM.onpointercancel = function(e) {
return _this10.handleButtonMouseUp();
buttonDOM.onpointercancel = function() {
return _this10.handleButtonMouseUp(button);
};
} else {
/**
@ -1700,11 +1700,11 @@
_this10.handleButtonClicked(button);
_this10.handleButtonMouseDown(button, e);
};
buttonDOM.ontouchend = function(e) {
return _this10.handleButtonMouseUp();
buttonDOM.ontouchend = function() {
return _this10.handleButtonMouseUp(button);
};
buttonDOM.ontouchcancel = function(e) {
return _this10.handleButtonMouseUp();
buttonDOM.ontouchcancel = function() {
return _this10.handleButtonMouseUp(button);
};
} else {
/**
@ -1714,7 +1714,10 @@
_this10.handleButtonClicked(button);
};
buttonDOM.onmousedown = function(e) {
_this10.handleButtonMouseDown(button, e);
return _this10.handleButtonMouseDown(button, e);
};
buttonDOM.onmouseup = function() {
return _this10.handleButtonMouseUp(button);
};
}
}
@ -1767,10 +1770,10 @@
} else if (useTouchEvents) {
/**
* Handling ontouchend, ontouchcancel
*/ document.ontouchend = function(e) {
*/ document.ontouchend = function() {
return _this10.handleButtonMouseUp();
};
document.ontouchcancel = function(e) {
document.ontouchcancel = function() {
return _this10.handleButtonMouseUp();
};
} else if (!useTouchEvents) {

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "simple-keyboard",
"version": "2.24.0",
"version": "2.24.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "simple-keyboard",
"version": "2.24.0",
"version": "2.24.1",
"description": "On-screen Javascript Virtual Keyboard",
"main": "build/index.js",
"types": "build/index.d.ts",

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 {

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

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");
});