Skip to content

Instantly share code, notes, and snippets.

@maxkrieger
Last active December 23, 2015 15:19
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 maxkrieger/6654720 to your computer and use it in GitHub Desktop.
Save maxkrieger/6654720 to your computer and use it in GitHub Desktop.
Amazing™ Active Keyboard Listener "great for games!"
// (raf shim by Paul Irish)
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
function detectKeys(){
if(keys[37] == true){
//left arrow stuffs
}
if (keys[39] == true){
//right arrow stuffs
}
if(keys[38] == true){
//up arrow stuffs
}
requestAnimationFrame(detectKeys);
}
var keys = [];
detectKeys();
window.addEventListener('keydown', function (e) {
keys[e.keyCode] = true;
}, false);
window.addEventListener('keyup', function (e) {
keys[e.keyCode] = false;
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment