Merge pull request #35 from MadDeveloper/tactile-touch-events

feat(Keyboard): add touch events (by @MadDeveloper)
This commit is contained in:
Francisco Hodge 2018-11-16 00:26:12 -05:00 committed by GitHub
commit 3086a45295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 9 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -635,6 +635,7 @@ class SimpleKeyboard {
let layoutClass = this.options.layout ? "hg-layout-custom" : `hg-layout-${this.options.layoutName}`; let layoutClass = this.options.layout ? "hg-layout-custom" : `hg-layout-${this.options.layoutName}`;
let layout = this.options.layout || KeyboardLayout.getDefaultLayout(); let layout = this.options.layout || KeyboardLayout.getDefaultLayout();
let useTouchEvents = this.options.useTouchEvents || false
/** /**
* Account for buttonTheme, if set * Account for buttonTheme, if set
@ -700,12 +701,22 @@ class SimpleKeyboard {
*/ */
var buttonDOM = document.createElement('div'); var buttonDOM = document.createElement('div');
buttonDOM.className += `hg-button ${fctBtnClass}${buttonThemeClass ? " "+buttonThemeClass : ""}`; buttonDOM.className += `hg-button ${fctBtnClass}${buttonThemeClass ? " "+buttonThemeClass : ""}`;
buttonDOM.onclick = () => {
this.isMouseHold = false; if (useTouchEvents) {
this.handleButtonClicked(button); buttonDOM.ontouchstart = (e) => {
this.handleButtonClicked(button);
this.handleButtonMouseDown(button, e);
}
buttonDOM.ontouchend = e => this.handleButtonMouseUp();
buttonDOM.ontouchcancel = e => this.handleButtonMouseUp();
} else {
buttonDOM.onclick = () => {
this.isMouseHold = false;
this.handleButtonClicked(button);
}
buttonDOM.onmousedown = (e) => this.handleButtonMouseDown(button, e);
} }
buttonDOM.onmousedown = (e) => this.handleButtonMouseDown(button, e);
/** /**
* Adding identifier * Adding identifier
*/ */
@ -759,7 +770,9 @@ class SimpleKeyboard {
/** /**
* Handling mouseup * Handling mouseup
*/ */
document.onmouseup = () => this.handleButtonMouseUp(); if (!useTouchEvents) {
document.onmouseup = () => this.handleButtonMouseUp();
}
if(!this.initialized){ if(!this.initialized){
/** /**

View File

@ -45,6 +45,31 @@ it('Keyboard will run with debug option set', () => {
expect(keyboard.options.debug).toBeTruthy(); expect(keyboard.options.debug).toBeTruthy();
}); });
it('Keyboard will use touch events', () => {
let touched = false
testUtil.clear()
document.body.innerHTML = `
<div id="keyboard"></div>
`;
const keyboard = new Keyboard('#keyboard', {
useTouchEvents: true,
onChange: () => touched = true,
layout: {
default: ["q"]
}
});
keyboard.getButtonElement("q").ontouchstart();
keyboard.getButtonElement("q").ontouchend();
expect(keyboard.options.useTouchEvents).toBeTruthy();
expect(touched).toBeTruthy();
expect(keyboard.getInput()).toBe('q');
})
it('Keyboard standard buttons will work', () => { it('Keyboard standard buttons will work', () => {
testUtil.setDOM(); testUtil.setDOM();
let keyboard = new Keyboard({ let keyboard = new Keyboard({