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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap explorer's special key combinations

Status
Not open for further replies.

almoes

Programmer
Jan 8, 2003
291
US
Hi!

I have an html page and would like to trap the explorer's special key combinations, such as Fx or CTRL-x. I tried the following with no luck:

<body onKeyPress="trapKeys()">
<script>
function trapKeys() {

alert("key "+String(window.event.keyCode));
window.event.returnValue = false;
}
</script>

...

Anybody ever tried this?
thanxs,
alej
 
Your code works perfectly if you switch from using onKeyPress to using onKeyDown.

Cheers,
Jeff
 
Hey cool! now I can trap the keys, however for the Fx I can't avoid the execution of the default behavior. Is it

window.event.returnValue = false;

not working for these keys? thanxs!
cheers,
alej
 
almoes said:
I have an html page and would like to trap the explorer's special key combinations

You didn't mention that was what you wanted to do in your initial post. I don't think you can do that (for the likes of the function keys at least). I thought you wanted to get the codes for some other use.

Hmmm... can you change the code maybe? If so you could change the function key codes to, say, a return key code. Just a thought.

Jeff
 
Right, did not say what I wanted to do...just had the other code line.
Good idea, I will try it now.

cheers,
alej
 
I tried setting

window.event.keyCode=0

but it doesn't work :-[ any ideas?

cheers,
alej
 
I tried

window.event.returnValue = false;
and
window.event.cancelBubble = true;

but it doesn't work. :-[

cheers,
alej
 
When I said return false, I meant return false, not set window.event.return to false ;o)

Try returning false:

Code:
return false;

or

Code:
return(false);

from your event handler and see if that works.

Dan
 
But there's no event handler...that's what I tried to do in first place but I can only trap it when 'onKeyDown' and then run a process. I could not find any window event handler.

alej
 

What event handler? You've lost me completely. I haven't mentioned anythiing about an event handler.

All I want to know is, have you tried modifying your trapKeys() function to have the following:

Code:
return(false);

instead of what you showed above:

Code:
window.event.returnValue = false;

I don't think I can make myself any clearer than that.

Hope this helps,
Dan
 
Ok, got it now. Tried it but it still doesn't work :-[

cheers,
alej
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top