Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting specific key press

Status
Not open for further replies.

Monkfish

IS-IT--Management
Joined
Oct 11, 2001
Messages
7
Location
GB
Hi, I've been directed over here from the Dreamweaver forum who suggested the following code in respect to me wanting to display one out of ten images in a browser window dependent on keys 0 through 9 being pressed.

<script language=&quot;JavaScript&quot;>
<!--
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function keyDown(e) {
if (ns4) {
if (e.which == &quot;#&quot;)
{
alert(&quot;your img switch code here&quot;);
}
}
if (ie4) {
if (event.keyCode == &quot;#&quot;)
{
alert(&quot;your img switch code here&quot;);
}
}
}
document.onkeydown = keyDown
if (ns4) document.captureEvents(Event.KEYDOWN)
//-->
</SCRIPT>

However, I'm a newbie programmer, so how would I use this to do what I want? I usually use image rollover code (as below) to swap between two images, but don't understand how I could mod it using the above code.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
<!--
function MM_swapImgRestore() { //v2.0
if (document.MM_swapImgData != null)
for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)
document.MM_swapImgData.src = document.MM_swapImgData[i+1];
}

function MM_preloadImages() { //v2.0
if (document.images) {
var imgFiles = MM_preloadImages.arguments;
if (document.preloadArray==null) document.preloadArray = new Array();
var i = document.preloadArray.length;
with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!=&quot;#&quot;){
preloadArray = new Image;
preloadArray[i++].src = imgFiles[j];
} }
}

function MM_swapImage() { //v2.0
var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;
for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {
objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
(objStr.indexOf('document.all[') ==0 && document.all ==null))
objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
obj = eval(objStr);
if (obj != null) {
swapArray[j++] = obj;
swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
obj.src = MM_swapImage.arguments[i+2];
} }
document.MM_swapImgData = swapArray; //used for restore
}
//-->
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; onLoad=&quot;MM_preloadImages('picture2.jpg','#1002812954010')&quot;>
<a href=&quot;#&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('document.Image1','document.Image1','picture2.jpg','#1002812954010')&quot;><img name=&quot;Image1&quot; border=&quot;0&quot; src=&quot;picture1.jpg&quot; width=&quot;512&quot; height=&quot;384&quot;></a>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top