Created
July 30, 2017 17:21
-
-
Save prof3ssorSt3v3/4e81ae914a37a83492564387f75d3c2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Keyboard events in the Browser | |
//ev.char || ev.charCode || ev.which | |
// keydown keyup keypress | |
let log = console.log; | |
document.addEventListener('DOMContentLoaded', init); | |
function init(){ | |
let txt = document.getElementById('txt'); | |
txt.addEventListener('keydown', anyKey); | |
document.body.addEventListener('keydown', anyKey); | |
} | |
function anyKey(ev){ | |
//log( ev.type, ev.target); | |
let target = ev.currentTarget; | |
let tag = target.tagName; | |
let char = ev.char || ev.charCode || ev.which; | |
//log(char, tag); | |
let s = String.fromCharCode(char); | |
//log(s); | |
switch(char){ | |
case 37: | |
log('LEFT'); | |
break; | |
case 40: | |
log('DOWN'); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment