Added disableButtonHold option

This commit is contained in:
Francisco Hodge 2019-06-05 21:08:53 -04:00
parent 5fa612a923
commit 04a3c186f0
3 changed files with 33 additions and 15 deletions

View File

@ -123,6 +123,11 @@ declare module 'simple-keyboard' {
*/ */
useMouseEvents?: boolean; useMouseEvents?: boolean;
/**
* Disable button hold action.
*/
disableButtonHold?: boolean;
/** /**
* Executes the callback function on key press. Returns button layout name (i.e.: "{shift}"). * Executes the callback function on key press. Returns button layout name (i.e.: "{shift}").
*/ */

View File

@ -65,6 +65,7 @@ class SimpleKeyboard {
* @property {boolean} autoUseTouchEvents Enable useTouchEvents automatically when touch device is detected. * @property {boolean} autoUseTouchEvents Enable useTouchEvents automatically when touch device is detected.
* @property {boolean} useMouseEvents Opt out of PointerEvents handling, falling back to the prior mouse event logic. * @property {boolean} useMouseEvents Opt out of PointerEvents handling, falling back to the prior mouse event logic.
* @property {function} destroy Clears keyboard listeners and DOM elements. * @property {function} destroy Clears keyboard listeners and DOM elements.
* @property {boolean} disableButtonHold Disable button hold action.
*/ */
this.options = options; this.options = options;
this.options.layoutName = this.options.layoutName || "default"; this.options.layoutName = this.options.layoutName || "default";
@ -245,22 +246,24 @@ class SimpleKeyboard {
/** /**
* @type {object} Time to wait until a key hold is detected * @type {object} Time to wait until a key hold is detected
*/ */
this.holdTimeout = setTimeout(() => { if (!this.options.disableButtonHold) {
if ( this.holdTimeout = setTimeout(() => {
this.isMouseHold && if (
((!button.includes("{") && !button.includes("}")) || this.isMouseHold &&
button === "{delete}" || ((!button.includes("{") && !button.includes("}")) ||
button === "{backspace}" || button === "{delete}" ||
button === "{bksp}" || button === "{backspace}" ||
button === "{space}" || button === "{bksp}" ||
button === "{tab}") button === "{space}" ||
) { button === "{tab}")
if (this.options.debug) console.log("Button held:", button); ) {
if (this.options.debug) console.log("Button held:", button);
this.handleButtonHold(button, e); this.handleButtonHold(button, e);
} }
clearTimeout(this.holdTimeout); clearTimeout(this.holdTimeout);
}, 500); }, 500);
}
} }
/** /**

View File

@ -1232,4 +1232,14 @@ it('Keyboard destroy will work', () => {
keyboard.destroy(); keyboard.destroy();
expect(keyboard.keyboardDOM.innerHTML).toBeFalsy(); expect(keyboard.keyboardDOM.innerHTML).toBeFalsy();
});
it('Keyboard disableButtonHold will work', () => {
testUtil.setDOM();
let keyboard = new Keyboard({
disableButtonHold: true
});
expect(keyboard.options.disableButtonHold).toBe(true);
}); });