Add key press handler

This commit is contained in:
Jeremy Likness 2019-11-23 12:20:09 -08:00
parent 88f7b5cbc2
commit 3a59e39fb0
4 changed files with 32 additions and 2 deletions

View File

@ -65,7 +65,6 @@ img {
slide-deck div {
height: 80vh;
padding: 1em;
border: solid 2px lightgray;
}
@media only screen and (max-width: 960px) {

View File

@ -20,6 +20,7 @@
<h1>DevNexus | Vanilla.js: Modern 1st Party JavaScript</h1>
<h2>Setting things up ...</h2>
</slide-deck>
<key-handler deck="main"></key-handler>
<div class="footer left">
<img src="./images/devnexus.png" alt="DevNexus logo" title="DevNexus logo" />
</div>

View File

@ -1,10 +1,11 @@
import { registerDeck } from "./navigator.js"
import { registerControls } from "./controls.js"
import { registerKeyHandler } from "./keyhandler.js"
const app = async () => {
registerDeck();
registerControls();
registerKeyHandler();
};
document.addEventListener("DOMContentLoaded", app);

29
js/keyhandler.js Normal file
View File

@ -0,0 +1,29 @@
class KeyHandler extends HTMLElement {
constructor() {
super();
this._deck = null;
}
static get observedAttributes() {
return ["deck"];
}
async attributeChangedCallback(attrName, oldVal, newVal) {
if (attrName === "deck") {
if (oldVal !== newVal) {
this._deck = document.getElementById(newVal);
this._deck.parentElement.addEventListener("keydown", key => {
if (key.keyCode == 39 || key.keyCode == 32) {
this._deck.next();
}
else if (key.keyCode == 37) {
this._deck.previous();
}
});
}
}
}
}
export const registerKeyHandler = () => customElements.define('key-handler', KeyHandler);