Here is what you need to do conceptually (I think this is called pseudocode):
Start with something like this:
var PIXEL_DELTA = 10;
Then, in each keydown event keydown check if the left , up , etc. key is pressed, and rotate its variable from 0 to PIXEL_DELTA .
In each keyup event keyup run the same test and return the correct variable back to 0 .
Then in your moving code (real code): (This code is adapted from the Crescent Fresh awesome example):
function move() { var dot = document.getElementById('dot'), deltaX = rightPressed - leftPressed, deltaY = downPressed - upPressed; if(deltaX) { dot.style.left = (parseInt(dot.style.left, 10) || 0) + deltaX + 'px'; } if (deltaY) { dot.style.top = (parseInt(dot.style.top, 10) || 0) + deltaY + 'px'; } }
The browser will (should) fire a separate keydown / keyup for each key, even if they are "pressed" simultaneously.
Working example
Crescent Fresh has compiled a complete JSBin example . Be sure to visit the editable version to play with the code.
Doug neiner
source share