Last active
December 23, 2015 15:19
-
-
Save maxkrieger/6654720 to your computer and use it in GitHub Desktop.
Amazing™ Active Keyboard Listener "great for games!"
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
// (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