Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created July 30, 2017 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/4e81ae914a37a83492564387f75d3c2e to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/4e81ae914a37a83492564387f75d3c2e to your computer and use it in GitHub Desktop.
// 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