From 04a3c186f0c75586bc866d782aa7d4520b4f0335 Mon Sep 17 00:00:00 2001 From: Francisco Hodge Date: Wed, 5 Jun 2019 21:08:53 -0400 Subject: [PATCH] Added disableButtonHold option --- src/lib/@types/index.d.ts | 5 ++++ src/lib/components/Keyboard.js | 33 ++++++++++++----------- src/lib/components/tests/Keyboard.test.js | 10 +++++++ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/lib/@types/index.d.ts b/src/lib/@types/index.d.ts index 41bf6e52..a4a703eb 100644 --- a/src/lib/@types/index.d.ts +++ b/src/lib/@types/index.d.ts @@ -123,6 +123,11 @@ declare module 'simple-keyboard' { */ useMouseEvents?: boolean; + /** + * Disable button hold action. + */ + disableButtonHold?: boolean; + /** * Executes the callback function on key press. Returns button layout name (i.e.: "{shift}"). */ diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index aee9999f..5caab5d5 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -65,6 +65,7 @@ class SimpleKeyboard { * @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 {function} destroy Clears keyboard listeners and DOM elements. + * @property {boolean} disableButtonHold Disable button hold action. */ this.options = options; this.options.layoutName = this.options.layoutName || "default"; @@ -245,22 +246,24 @@ class SimpleKeyboard { /** * @type {object} Time to wait until a key hold is detected */ - this.holdTimeout = setTimeout(() => { - if ( - this.isMouseHold && - ((!button.includes("{") && !button.includes("}")) || - button === "{delete}" || - button === "{backspace}" || - button === "{bksp}" || - button === "{space}" || - button === "{tab}") - ) { - if (this.options.debug) console.log("Button held:", button); + if (!this.options.disableButtonHold) { + this.holdTimeout = setTimeout(() => { + if ( + this.isMouseHold && + ((!button.includes("{") && !button.includes("}")) || + button === "{delete}" || + button === "{backspace}" || + button === "{bksp}" || + button === "{space}" || + button === "{tab}") + ) { + if (this.options.debug) console.log("Button held:", button); - this.handleButtonHold(button, e); - } - clearTimeout(this.holdTimeout); - }, 500); + this.handleButtonHold(button, e); + } + clearTimeout(this.holdTimeout); + }, 500); + } } /** diff --git a/src/lib/components/tests/Keyboard.test.js b/src/lib/components/tests/Keyboard.test.js index d3c4501d..b37f6e0e 100644 --- a/src/lib/components/tests/Keyboard.test.js +++ b/src/lib/components/tests/Keyboard.test.js @@ -1232,4 +1232,14 @@ it('Keyboard destroy will work', () => { keyboard.destroy(); expect(keyboard.keyboardDOM.innerHTML).toBeFalsy(); +}); + +it('Keyboard disableButtonHold will work', () => { + testUtil.setDOM(); + + let keyboard = new Keyboard({ + disableButtonHold: true + }); + + expect(keyboard.options.disableButtonHold).toBe(true); }); \ No newline at end of file