Cross browser key handler?

C

Ciaran

Hi has anyone got a function that can detect a user pressing a key
that works cross browser?
I just want to redirect my visitor if they hit escape ... you'd think
it would be easy!
Thanks,
Ciarán
 
C

Ciaran

Hi has anyone got a function that can detect a user pressing a key
that works cross browser?
I just want to redirect my visitor if they hit escape ... you'd think
it would be easy!
Thanks,
Ciarán

Anyone?
 
S

scripts.contact

Hi has anyone got a function that can detect a user pressing a key
that works cross browser?
I just want to redirect my visitor if they hit escape

try:

document.body.onkeypress=
function(e){
if((e||event).keyCode==27)
location.href="http://google.com";
}

untested
 
C

Ciaran

try:

document.body.onkeypress=
function(e){
if((e||event).keyCode==27)
location.href="http://google.com";

}

untested


Thanks for the reply! .... but it doesnt seem to be working. I have
this function working in IE but FF is ignoring it ...

if (document.layers) { document.captureEvents(Event.KEYPRESS); }
document.onkeypress = getKey;
function getKey(keyStroke) {
var keyCode = (document.layers) ? keyStroke.which : event.keyCode;
if(keyCode==27) location.href='quit.html';
}
 
S

scripts.contact

Thanks for the reply! .... but it doesnt seem to be working. I have
this function working in IE but FF is ignoring it ...

Try this:

document.onkeypress=changeHREF
function changeHREF(ev){
ev=ev||event;
if(ev.keyCode==27){
location.href="http://google.com"
return false
}
}

works in o9,f2,i6
 
C

Ciaran

Try this:

document.onkeypress=changeHREF
function changeHREF(ev){
ev=ev||event;
if(ev.keyCode==27){
location.href="http://google.com"
return false
}
}

works in o9,f2,i6

Wow! perfect! and such an elegant solution! I searched the net for a
key handler like this and everything I found was either 2 pages long
or only worked in ie. Thanks a lot!
Ciarán
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top