I am using JQuery to monitor key presses for an HTML game, but whenever I add the jquery code to my gameloop then I get an error about keydown not being defined.
<html>
<head>
//...Included JS files JQuery, and a JQuery plugin to bind keydown
// to a variable and to translate all the keypresses into
// names so that we can monitor keypress by keydown.left...
</head>
<body>
<script type="text/javascript">
var gLoop;
//...code excluded for brevity...
var GameLoop = function() {
//...code excluded...
if(keydown.left) {
//do stuff
}
gLoop = setTimeout(GameLoop, 1000/50);
}
</script>
</body>
</html>
This code gets an error saying keydown is undefined. When ever I tried this code:
setInterval(function(){
if(keydown.left) alert("Working");
}, 1000/50);
It works fine!!! What am I doing wrong??? My full code is on GitHub here.
I used jQuery version 1.4.4. I used the hotkeys plugin which is available at https://raw.github.com/figitaki/PuzzleMan/master/key_status.js And here is the code I used to bind keydown:
$(function() {
window.keydown = {};
function keyName(event) {
return jQuery.hotkeys.specialKeys[event.which] ||
String.fromCharCode(event.which).toLowerCase();
}
$(document).bind("keydown", function(event) {
keydown[keyName(event)] = true;
});
$(document).bind("keyup", function(event) {
keydown[keyName(event)] = false;
});
});
UPDATE:
I no longer get an error when I run the code but I do not get any response by pressing the left key.