PDA

View Full Version : onkeypress


mattover-matter
04-17-2003, 08:35 AM
Is there anyway to have it where u hit a key it clicks a link?
like

Home

Caffeine
04-17-2003, 09:20 AM
You can not make the browser click a link, that would be a security issue.

You can simulate a click though by putting this script into the HEAD-section of the page: (this example redirect the user to the page when the button H (or h) has been pressed.

<SCRIPT>

function gotoURL() {
if (event.keyCode == 72 || event.keyCode == 104 ) {
location.href="yourpagehere.html";
}
}

document.onkeypress = gotoURL;

</SCRIPT>

-phleg-

brothercake
04-17-2003, 11:26 AM
Originally posted by phlegmatic
You can not make the browser click a link
Yes you can ... eg:

<a href="something.html" id="myLink">something</a>

then

document.getElementById("myLink").click()


But HTML already has a mechanism for activating links from keypresses - it's called accesskey. See this thread (http://www.codingforums.com/showthread.php?s=&threadid=18396)

Caffeine
04-17-2003, 05:34 PM
Ahh, nice. I did not know that.
I've never seen it 'in action' yet, what browsers understand that code ?

-phleg-

beetle
04-17-2003, 05:55 PM
Durn it bcake, you beat me to it! :D

-----

Well, looking solely at the click() method, support goes as follows

IE4+ for most any element (3+ for <a> and <area>)

Gecko (Mozilla, Netscape) For INPUT elements whose type attribute has one of the following values: "Button", "Checkbox", "Radio", "Reset", or "Submit".

KHTML/Opera - assumed to be same as Gecko (which follows the W3 spec)

NS4 - no clue, probably not.

Since Gecko doesn't support click() on just any element, you'd have to prototype it on, which jkd showed me a while back HTMLElement.prototype.click = function()
{
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}

mattover-matter
04-18-2003, 12:13 AM
What are the "keycodes" for all the keys. Every link must have a button :-)

Is there somepage with a list?

a:
b:
c:
d:
e:
f:
g:

etc. etc.

chrismiceli
04-18-2003, 02:24 AM
like this

Originally posted by A1ien51
you could just have them alerted like it does here...

http://www10.brinkster.com/A1ien51/Scripts/KeyCode.htm