11/29/10

Handling Keyboard Events in Javascript

Guys if u have seen the moving of pictures on pressing a key in FB , FLICKR etc. Can also be done by you all now.
The code for Handling the keyboard button press etc is shown below



**********************##***********************
document.onkeydown = keyHit;
var thisPic = 0;
function keyHit(evt) {
var myPix = new Array("List down your images here (ex. "images/callisto.jpg",
               "images/callisto123.jpg")");
var imgCt = myPix.length-1;
var ltArrow = 37;
var rtArrow = 39;
var thisKey;
if (evt) {
thisKey = evt.which;
}else {
thisKey = window.event.keyCode;
}
if (thisKey == ltArrow) {
chgSlide(-1);
}else {
if (thisKey == rtArrow) {
chgSlide(1);
}
}
return false;
function chgSlide(direction) {
thisPic = thisPic + direction;
if (thisPic > imgCt) {
thisPic = 0;
}
if (thisPic < 0) {
thisPic = imgCt;
}
document.getElementById("myPicture").src = myPix[thisPic];
}
}


SHARE THIS POST: