Skip to content

Instantly share code, notes, and snippets.

@jaz303
Created January 15, 2011 23:17
Show Gist options
  • Save jaz303/781358 to your computer and use it in GitHub Desktop.
Save jaz303/781358 to your computer and use it in GitHub Desktop.
Key State
<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
<script type='text/javascript'>
var states = {};
// w - 87
// a - 65
// s - 83
// d - 68
$(document).bind('keydown', function(evt) {
states[evt.which] = true;
}).bind('keyup', function(evt) {
states[evt.which] = false;
});
setInterval(function() {
var state = "";
if (states[87]) state += "FWD ";
if (states[83]) state += "BACK ";
if (states[65]) state += "LEFT ";
if (states[68]) state += "RIGHT ";
if (state.length) {
console.log(state);
}
}, 500);
</script>
</head>
<body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment