...

View Full Version : HELP WITH onkeydown()



HiEverybody
07-13-2004, 01:30 AM
Goodevening

function Keypressed()
{
window.location = "cart.html";
}

This is how I call the function:

<p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>


It works fine but I can press any key and it goes to the linked page.
I only want it to respond on the letter "v"


How can I change this code so that when I press the letter "v" on the keyboard it access the link?

Hoping to hear from someone?

carbootsale
07-13-2004, 02:22 AM
try using

onKeyDown="Keypressed(event.keyCode);"

You can then test the value of keyCode in your function

function Keypressed(keyCode)
{
if (keyCode==86)
{
window.location = "cart.html";
}
}

SpiritualStorms
07-13-2004, 05:25 AM
I was looking at your code, and well, i noticed, that you didnt pass any specific details to your function at the time of the functions activation.



Goodevening

function Keypressed()
{
window.location = "cart.html";
}

This is how I call the function:

<p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>


It works fine but I can press any key and it goes to the linked page.
I only want it to respond on the letter "v"


How can I change this code so that when I press the letter "v" on the keyboard it access the link?


You have defined a function above, but you have not given the function any values, or variables. For this reason, when you call your function, all it will do, is take a person to a web-file, since the web-file is the only thing assigned as a value to the property/object location. For this reason, if you want the function to respond to a very specific event, like the actual pressing of the V key on the keyboard, you will have to set up your function in such a way, that it will respond only to that event, and not to any other. Here's what you have:


<p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>


You have chosen to place your function inside a link tag. For this reason, when you get a visitor to click on the link, the only thing that will be activated is the function, even though, in your example, you have said, onKeyDown. You have the right event-handler, but the wrong set up: you are using a link tag to call a function.

Additionally, when someone clicks on the link, they will go to "cart.html" because you have given that addy as a value to the href property of a tag. There's no reason why you should be having that if you are wanting a function to control the location property. This is why a visitor is always going to get the addy of "cart.html". To just use the link tag for the place of a function, but you do not want the link to act as an actual link, then you should be doing something like "href="#". If you dont do this, then the link will always act as a link. A function is generally used for when you want to seperate, and control specific lines of code, code that needs to stand on its own from other code. That's the whole reason for using a function, nothing more.

Have you read any books, or gone to any sites?

HiEverybody
07-13-2004, 10:32 AM
Thank You Both for your Help!!

Cheers

HiEverybody
07-13-2004, 11:15 AM
Attention: Carbootsale

I tried your code it works but not when the page first opens.

I have to click on the "here" link and then when i return to my order page then I can press the "v" key on the keyboard.

It only works after I have gone to the shopping cart page and returned to my order page.

Here is your code and it works only with the little proble described above:




onKeyDown="Keypressed(event.keyCode);"


function Keypressed(keyCode)
{
if (keyCode==86)
{
window.location = "cart.html";
}
}



It has something to do with the placement of the onKeyDown="Keypressed(event.keyCode);" event handler

dumpfi
07-13-2004, 04:40 PM
document.onkeydown = keypressed;
function keypressed(e) {
e = e || window.event;
var key = String.fromCharCode(e.keyCode).toUpperCase();
if(key == "V") {
window.location.href = "cart.html";
}
}



<p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html">here</a></p>

dumpfi

HiEverybody
07-13-2004, 08:44 PM
You are super cool my friend and I thank you for your help.

Your a lifesaver!!!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum