mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2026-04-30 00:00:04 +08:00
Demo update
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import Keyboard from "../lib";
|
||||
import "./css/MultipleKeyboardsDestroyDemo.css";
|
||||
|
||||
const setDOM = () => {
|
||||
document.querySelector("#root").innerHTML = `
|
||||
<input class="input" placeholder="Tap on the virtual keyboard to start" />
|
||||
<div class="simple-keyboard"></div>
|
||||
|
||||
<input class="input2" placeholder="Tap on the virtual keyboard to start" />
|
||||
<div class="keyboard2"></div>
|
||||
`;
|
||||
};
|
||||
|
||||
class Demo {
|
||||
constructor() {
|
||||
setDOM();
|
||||
|
||||
/**
|
||||
* Demo Start
|
||||
*/
|
||||
this.keyboard = new Keyboard({
|
||||
onChange: input => this.onChange(input),
|
||||
onKeyPress: button => this.onKeyPress(button),
|
||||
debug: true
|
||||
});
|
||||
|
||||
this.keyboard2 = new Keyboard(".keyboard2", {
|
||||
theme: "simple-keyboard hg-theme-default",
|
||||
onChange: input => this.onChange(input, "input2"),
|
||||
onKeyPress: button => this.onKeyPress(button, "keyboard2"),
|
||||
debug: true
|
||||
});
|
||||
|
||||
console.log(this.keyboard);
|
||||
setTimeout(this.keyboard.destroy, 10000);
|
||||
|
||||
/**
|
||||
* Update simple-keyboard when input is changed directly
|
||||
*/
|
||||
document.querySelector(".input").addEventListener("input", event => {
|
||||
this.keyboard.setInput(event.target.value);
|
||||
});
|
||||
|
||||
document.querySelector(".input2").addEventListener("input", event => {
|
||||
this.keyboard2.setInput(event.target.value);
|
||||
});
|
||||
}
|
||||
|
||||
onChange(input, inputClass) {
|
||||
document.querySelector(`.${inputClass || "input"}`).value = input;
|
||||
console.log("Input changed", input);
|
||||
}
|
||||
|
||||
onKeyPress(button, keyboardInstanceKey) {
|
||||
console.log("Button pressed", button);
|
||||
|
||||
/**
|
||||
* If you want to handle the shift and caps lock buttons
|
||||
*/
|
||||
if (button === "{shift}" || button === "{lock}")
|
||||
this.handleShift(keyboardInstanceKey);
|
||||
}
|
||||
|
||||
handleShift(keyboardInstanceKey) {
|
||||
let keyboard = this[keyboardInstanceKey || "keyboard"];
|
||||
let currentLayout = keyboard.options.layoutName;
|
||||
let shiftToggle = currentLayout === "default" ? "shift" : "default";
|
||||
|
||||
keyboard.setOptions({
|
||||
layoutName: shiftToggle
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Demo;
|
||||
Reference in New Issue
Block a user