- Sep 15, 2005
- 3,622
- 1
- 0
I am having difficulty handling the situation where a key / keys are held down.
My javascript code here works to enable me to move an object around in the div it is contained in, but when I hold one of the arrow keys down I only get one event fired off, and not multiple repeated events. I want the object to continue to move while the key is held down.
Anyone know how to handle this properly?
The relevant part of the code begins with: window.addEvent('keyup' , function(event) {
My javascript code here works to enable me to move an object around in the div it is contained in, but when I hold one of the arrow keys down I only get one event fired off, and not multiple repeated events. I want the object to continue to move while the key is held down.
Anyone know how to handle this properly?
The relevant part of the code begins with: window.addEvent('keyup' , function(event) {
Code:
window.addEvent("domready", function() {
var duration = 40000;
var length = 2000;
var count = 0;
var tweener;
var run = function() {
tweener.tween("background-position", "-" + (++count * length) + "px 0px");
};
//defines a tween
tweener = $("animate-area").setStyle("background-position", "0px 0px").set("tween", {
duration: duration,
transition: Fx.Transitions.linear,
onComplete: run,
link: "cancel"
});
window.addEvent('keyup' , function(event) {
var shipX = $("ship").getStyle("margin-left");
var shipY = $("ship").getStyle("margin-top");
//alert(event.key);
var newy, newx;
//alert("ShipX: " + shipX + " ShipY: " + shipY);
switch(event.key){
case "up":
newy = parseInt(shipY, 10) - 5;
newx = parseInt(shipX, 10);
break;
case "down":
newy = parseInt(shipY, 10) + 5;
newx = parseInt(shipX, 10);
break;
case "left":
newy = parseInt(shipY, 10);
newx = parseInt(shipX, 10) - 5;
break;
case "right":
newy = parseInt(shipY, 10);
newx = parseInt(shipX, 10) + 5;
break;
case "space":
break;
default:
return;
break;
}
//alert("newy: " + newy + " newx: " + newx);
var shiptween = $("ship").setStyles({
"margin-top" : newy + "px",
"margin-left" : newx + "px"
}).set("tween", {
duration: 100,
transition: Fx.Transitions.linear,
link: "cancel"
});
});
//start initial run of transition
run();
});