It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

JavaScript Keyboard Event

JavaScript Keyboard Event

Postby DeThomasso on Wed Apr 06, 2011 5:21 pm

Hey...I need help with Keyboard events exactly arrows...I used many examples from the internet but I can't find the root of the problem :?: :?: :?: :( . I just need to load the various inputs from the keyboard (arrows) for controlling ...like in games...

this is the code I used: (I just tested it, if it reacts to events)
<script type="text/javascript" language="JavaScript">
var a=document;

function spust(){
rightDown = false;
leftDown = false;

//set rightDown or leftDown if the right or left keys are down
function onKeyDown(evt) {
if (evt.keyCode == 39) rightDown = true;
else if (evt.keyCode == 37) leftDown = true;
}

//and unset them when the right or left key is released
function onKeyUp(evt) {
if (evt.keyCode == 39) rightDown = false;
else if (evt.keyCode == 37) leftDown = false;
}

$(document).keydown(onKeyDown);
//$ (window).keydown(onKeyDown);
$(document).keyup(onKeyUp);
//$ (window).keyup(onKeyUp);

setInterval(function() {

//move the paddle if left or right is currently pressed
if (rightDown) alert('Right');
else if (leftDown) alert('Left');
clearInterval(intervalId);

}, 10);}
</script>
DeThomasso
 
Posts: 1
Joined: Wed Apr 06, 2011 5:06 pm

Who is online

Users browsing this forum: No registered users and 6 guests