mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2026-04-11 00:02:15 +08:00
156 lines
6.5 KiB
HTML
156 lines
6.5 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<base data-ice="baseUrl" href="../../../../">
|
|
<title data-ice="title">src/lib/services/PhysicalKeyboard.js | simple-keyboard</title>
|
|
<link type="text/css" rel="stylesheet" href="css/style.css">
|
|
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
|
|
<script src="script/prettify/prettify.js"></script>
|
|
<script src="script/manual.js"></script>
|
|
<meta name="description" content="On-screen Javascript Virtual Keyboard"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="simple-keyboard"><meta property="twitter:description" content="On-screen Javascript Virtual Keyboard"></head>
|
|
<body class="layout-container" data-ice="rootContainer">
|
|
|
|
<header>
|
|
<a href="./">Home</a>
|
|
|
|
<a href="identifiers.html">Reference</a>
|
|
<a href="source.html">Source</a>
|
|
|
|
<div class="search-box">
|
|
<span>
|
|
<img src="./image/search.png">
|
|
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
|
|
</span>
|
|
<ul class="search-result"></ul>
|
|
</div>
|
|
<a style="position:relative; top:3px;" href="https://github.com/hodgef/simple-keyboard"><img width="20px" src="./image/github.png"></a></header>
|
|
|
|
<nav class="navigation" data-ice="nav"><div>
|
|
<ul>
|
|
|
|
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#demo">demo</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/demo/App.js~App.html">App</a></span></span></li>
|
|
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#lib-components">lib/components</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/lib/components/Keyboard.js~SimpleKeyboard.html">SimpleKeyboard</a></span></span></li>
|
|
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#lib-services">lib/services</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/lib/services/KeyboardLayout.js~KeyboardLayout.html">KeyboardLayout</a></span></span></li>
|
|
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/lib/services/PhysicalKeyboard.js~PhysicalKeyboard.html">PhysicalKeyboard</a></span></span></li>
|
|
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/lib/services/Utilities.js~Utilities.html">Utilities</a></span></span></li>
|
|
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#lib-tests">lib/tests</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/lib/tests/TestUtility.js~TestUtility.html">TestUtility</a></span></span></li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="content" data-ice="content"><h1 data-ice="title">src/lib/services/PhysicalKeyboard.js</h1>
|
|
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">/**
|
|
* Physical Keyboard Service
|
|
*/
|
|
class PhysicalKeyboard {
|
|
/**
|
|
* Creates an instance of the PhysicalKeyboard service
|
|
*/
|
|
constructor(simpleKeyboardInstance){
|
|
/**
|
|
* @type {object} A simple-keyboard instance
|
|
*/
|
|
this.simpleKeyboardInstance = simpleKeyboardInstance;
|
|
|
|
/**
|
|
* Bindings
|
|
*/
|
|
this.initKeyboardListener = this.initKeyboardListener.bind(this);
|
|
this.getSimpleKeyboardLayoutKey = this.getSimpleKeyboardLayoutKey.bind(this);
|
|
|
|
/**
|
|
* Initialize key listeners
|
|
*/
|
|
this.initKeyboardListener();
|
|
}
|
|
|
|
/**
|
|
* Initializes key event listeners
|
|
*/
|
|
initKeyboardListener(){
|
|
// Adding button style on keydown
|
|
document.addEventListener("keydown", (event) => {
|
|
if(this.simpleKeyboardInstance.options.physicalKeyboardHighlight){
|
|
let buttonPressed = this.getSimpleKeyboardLayoutKey(event);
|
|
|
|
this.simpleKeyboardInstance.dispatch(instance => {
|
|
let buttonDOM = instance.getButtonElement(buttonPressed) || instance.getButtonElement(`{${buttonPressed}}`);
|
|
|
|
if(buttonDOM){
|
|
buttonDOM.style.backgroundColor = this.simpleKeyboardInstance.options.physicalKeyboardHighlightBgColor || "#9ab4d0";
|
|
buttonDOM.style.color = this.simpleKeyboardInstance.options.physicalKeyboardHighlightTextColor || "white";
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
// Removing button style on keyup
|
|
document.addEventListener("keyup", (event) => {
|
|
if(this.simpleKeyboardInstance.options.physicalKeyboardHighlight){
|
|
let buttonPressed = this.getSimpleKeyboardLayoutKey(event);
|
|
|
|
this.simpleKeyboardInstance.dispatch(instance => {
|
|
let buttonDOM = instance.getButtonElement(buttonPressed) || instance.getButtonElement(`{${buttonPressed}}`);
|
|
|
|
if(buttonDOM && buttonDOM.removeAttribute){
|
|
buttonDOM.removeAttribute("style");
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Transforms a KeyboardEvent's "key.code" string into a simple-keyboard layout format
|
|
* @param {object} event The KeyboardEvent
|
|
*/
|
|
getSimpleKeyboardLayoutKey(event){
|
|
let output;
|
|
|
|
if(
|
|
event.code.includes("Numpad") ||
|
|
event.code.includes("Shift") ||
|
|
event.code.includes("Space") ||
|
|
event.code.includes("Backspace") ||
|
|
event.code.includes("Control") ||
|
|
event.code.includes("Alt") ||
|
|
event.code.includes("Meta")
|
|
){
|
|
output = event.code;
|
|
} else {
|
|
output = event.key;
|
|
}
|
|
|
|
/**
|
|
* If button is not uppercase, casting to lowercase
|
|
*/
|
|
if (
|
|
output !== output.toUpperCase() ||
|
|
(event.code[0] === "F" && Number.isInteger(Number(event.code[1])) && event.code.length <= 3)
|
|
) {
|
|
output = output.toLowerCase();
|
|
}
|
|
|
|
return output;
|
|
}
|
|
}
|
|
|
|
export default PhysicalKeyboard;</code></pre>
|
|
|
|
</div>
|
|
|
|
<footer class="footer">
|
|
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
|
|
</footer>
|
|
|
|
<script src="script/search_index.js"></script>
|
|
<script src="script/search.js"></script>
|
|
<script src="script/pretty-print.js"></script>
|
|
<script src="script/inherited-summary.js"></script>
|
|
<script src="script/test-summary.js"></script>
|
|
<script src="script/inner-link.js"></script>
|
|
<script src="script/patch-for-local.js"></script>
|
|
</body>
|
|
</html>
|