discriminately capturing onkeypress events

J

jbigham

Hello,

I'd like to capture key events using javascript, but don't want to
process such events when the user is typing into an input box or into a
textarea. As an example, gmail has a feature where you can type "r"
anywhere on the page while viewing a message and it will open a reply
box, but if you type an "r" while entering text into a form, it doesn't
do this.

My question is how to do this most efficiently.

Should I capture all events of interest and then determine whether to
act based on the object that has focus?

Should I define many onkeypress events for each of the objects on the
page that aren't text boxes?

Should I define the onkeypress at page level but return null for
onkeypress from each of the text boxes on the page?

What do you think?

Thanks!
Jeff
 
C

Csaba Gabor

Hello,

I'd like to capture key events using javascript, but don't want to
process such events when the user is typing into an input box or into a
textarea. As an example, gmail has a feature where you can type "r"
anywhere on the page while viewing a message and it will open a reply
box, but if you type an "r" while entering text into a form, it doesn't
do this.

My question is how to do this most efficiently. ....
Should I define the onkeypress at page level but return null for
onkeypress from each of the text boxes on the page?

I would capture the 'keydown' event (there is also keyup and keypress),
and use the event object to see what the original target was. If it
was a textbox or textarea (which you can check using .tagName and
..type), then return immediately. Otherwise do your thing and
subsequently perhaps cancel the event from bubbling / continuing.

Csaba Gabor from Vienna
 
J

Jim

Jeff,
This should do the trick: type 'r' in the form input and text areas and
nothing will happen; type it anywhere in the body and the paragraph
text will change. this should work in both msie and firefox; also, I
found that onKeyup seems to be more browser compatible.
-------------------------------------------------------------
<script type='text/javascript' >
function showStuff(evt){
var node = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
evt = (evt) ? evt : ((event) ? event : null);
var keynumber = evt.keyCode;
if(keynumber == 82){
if((node.tagName =="INPUT")||(node.tagName =="TEXTAREA")){
//do nothing;
}else{
document.getElementById("para").innerHTML = "you pressed the R key";
}
}
}
</script>
</head>
<body onkeyup = "showStuff(event)" onClick="showStuff(event)">
<form>
<input type="text" id="entry" size = "60" ><br>
<textarea rows="7" cols="57"></textarea></p>
</form>
<p id="para">some paragraph text</p>
</body>
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top