Jump to content

handling KeyBoard events in JAVASCRIPT

- - - - -

  • Please log in to reply
No replies to this topic

#1
code2learn

code2learn

    Newbie

  • Members
  • Pip
  • 9 posts
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]; } } 

Just check the code and use it. for any queries mail me.

Edited by Roger, 01 January 2011 - 06:07 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users