Jump to content

how to report users key movements!


sooty2006

Recommended Posts

hi im making a menu on the left-hand side of my site,i have a working script i created but only at the moment i can only do it using "onchange"is there a way i can make it change without having to use a select boxkey.event() ? i havent a clueso instead of a select box i can change menu types by the user using the arrow keys on there keyboard?thanks.

Link to comment
Share on other sites

There are the onkeypress, onkeyup and onkeydown events.You can do something like this:

document.onkeypress = function(e) {  // Older versions of Internet Explorer use window.event  e = (e)?e:((window.event)?window.event:"");  // Detect which key was pressed  pressed = (e.keyCode)?e.keyCode:e.which;  if(pressed == 38) { //If the user pressed the up arrow key	// Perform an action.  } else if (pressed == 40) { //If the user pressed the down arrow key	// Perform an action  }}

Link to comment
Share on other sites

<script>    var amount = new Array('2','3','4','5');  var difficult = new Array('Easy','Medium','Hard','Hard');  var maxtime = new Array('2','4','6','6');  var nexoc = new Array('6','12','24','24');  var maxprofit = new Array('350000','2000000','3000000','4000000');    function changeRobbery(obj){   if (obj>=1 && obj<=4){	document.getElementById('amountplayers').innerHTML=amount[obj-1]; 	document.getElementById('difficulty').innerHTML=difficult[obj-1]; 	document.getElementById('maxtime').innerHTML=maxtime[obj-1]; 	document.getElementById('nextoc').innerHTML=nexoc[obj-1]; 	document.getElementById('maxprofit').innerHTML=maxprofit[obj-1]; 	document.getElementById('button').style.display="block";   }   else{	document.getElementById('amountplayers').innerHTML='-'; 	document.getElementById('difficulty').innerHTML='-'; 	document.getElementById('maxtime').innerHTML='-'; 	document.getElementById('nextoc').innerHTML='-'; 	document.getElementById('maxprofit').innerHTML='-'; 	document.getElementById('button').style.display="none";   }  }    </script>

thats my code but i want to use images in a row and on the right hand side show text for the item clicked on

Link to comment
Share on other sites

thanks Ingolmeill try it but where would i put this code in the javascript code i posted it before! im not enierly sure because i dont usually use javascript :)

Link to comment
Share on other sites

<select name="type" class="txt" style="width:125px" onchange="changeRobbery(this.value)">   <option value="0">Select</option>   <option value="1">lin1</option>   <option value="2">line2</option>   <option value="3">line3</option>   <option value="4">line4</option></select>

currently on a select statementbut i want to change from select to this is an exapmple btw item one

now a box containg info about item one then

item two

if they push down arrow it takes them to item 2 etc...

item threeitem fourblah, blah

Link to comment
Share on other sites

You could do it like this:

<script type="text/javascript">  var type = 0;  var amount = new Array('2','3','4','5');  var difficult = new Array('Easy','Medium','Hard','Hard');  var maxtime = new Array('2','4','6','6');  var nexoc = new Array('6','12','24','24');  var maxprofit = new Array('350000','2000000','3000000','4000000');  function changeRobbery(obj){    if (obj>=1 && obj<=4){      document.getElementById('amountplayers').innerHTML=amount[obj-1];      document.getElementById('difficulty').innerHTML=difficult[obj-1];      document.getElementById('maxtime').innerHTML=maxtime[obj-1];      document.getElementById('nextoc').innerHTML=nexoc[obj-1];      document.getElementById('maxprofit').innerHTML=maxprofit[obj-1];      document.getElementById('button').style.display="block";    } else {      document.getElementById('amountplayers').innerHTML='-';      document.getElementById('difficulty').innerHTML='-';      document.getElementById('maxtime').innerHTML='-';      document.getElementById('nextoc').innerHTML='-';      document.getElementById('maxprofit').innerHTML='-';      document.getElementById('button').style.display="none";    }  }  document.onkeypress = function(e) {    // Older versions of Internet Explorer use window.event    e = (e)?e:((window.event)?window.event:"");    // Detect which key was pressed    pressed = (e.keyCode)?e.keyCode:e.which;    if(pressed == 38) { //If the user pressed the up arrow key      type--;      type = (type < 0)?type = amount.length:type;      changeRobbery(type);    } else if (pressed == 40) { //If the user pressed the down arrow key      type++;      type = (type > 4)?0:type;      changeRobbery(type);    }  }</script>

I haven't tested to be sure it works, but it should, though I might have made a mistake somewhere.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...