This is the code I'm using now to move a multi-layer carousel (left right up down) but I need to add a timeout function for say 5000 so the animation has time to complete before the next click is triggered.
function checkKey(e){
switch (e.keyCode) {
case 40:
//alert("down");
$("#down").trigger("click");
break;
case 38:
//alert("up");
$("#up").trigger("click");
break;
case 37:
//alert("left");
$("#left").trigger("click");
break;
case 39:
//alert("right");
$("#right").trigger("click");
break;
}
}
// Call checkKey on key press
if ($.browser.mozilla) {
$(document).keypress(checkKey);
} else {
$(document).keydown(checkKey);
}
UPDATE:
I ended up finding a solution. I know there is probably a much cleaner way to do this but I don't know how so forgive my messiness.
// Set Time variable
var checkTime = 0;
// Call checkKey on key press
if ($.browser.mozilla) {
$(document.documentElement).keypress(function (event) {
// set variable to current time
var currentTime = new Date()
// See if Now's current time is 400 past the old current time
if((currentTime.getTime() - checkTime) > 400){
// If so then fire the triggers.
// Using if seemed to limit the inputs to a single keystroke
if (event.keyCode == 40) {
$("#down").trigger("click");
} else if (event.keyCode == 38) {
$("#up").trigger("click");
} else if (event.keyCode == 37) {
$("#left").trigger("click");
} else if (event.keyCode == 39) {
$("#right").trigger("click");
}
// Reset current time.
checkTime =currentTime.getTime();
}
});
} else {
$(document.documentElement).keydown(function (event) {
// set variable to current time
var currentTime = new Date()
// See if Now's current time is 400 past the old current time
if((currentTime.getTime() - checkTime) > 400){
// If so then fire the triggers.
// Using if seemed to limit the inputs to a single keystroke
if (event.keyCode == 40) {
$("#down").trigger("click");
} else if (event.keyCode == 38) {
$("#up").trigger("click");
} else if (event.keyCode == 37) {
$("#left").trigger("click");
} else if (event.keyCode == 39) {
$("#right").trigger("click");
}
// Reset current time.
checkTime =currentTime.getTime();
}
});
}