473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

discriminately capturing onkeypress events

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

Mar 28 '06 #1
2 2258
jb*****@cs.washington.edu wrote:
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

Mar 28 '06 #2
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>

Mar 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fred Brown | last post by:
Hi, I want to cancel a certain key in JavaScript. To do so, I catch the event in OnKeyPress and cancel the default: <head> .... function f(evt) { var evt = (evt) ? evt : ((window.event) ?...
3
by: Greg | last post by:
I want to be able to capture the user pressing Ctrl+S. I know that the IE browser has a key binding for Ctrl+S but is there any way that I can be notified of this key press anyway. I have found...
4
by: Jonas | last post by:
Hi! I have a web page where I want to intercept keypress events in an INPUT-tag and check if it is the Enter key, which calls another function that executes a search. My code runs on Netscape 7...
4
by: Jay Xx | last post by:
I have an IFrame in design mode. I've tried a bunch of things to capture key presses in that IFrame, but I can't seem to get it. I can capture key presses outside the IFrame fine. I have this...
7
by: jerrygarciuh | last post by:
Hello, I have been playing with various Googled solutions for capturing the <Enter> key to suppress form submission. My first question is whether anyone has a script that works in all common...
1
by: Jon Rea | last post by:
Hi, First of all is there/ what is the Archive URL of this newsgroup ? Secondly I want to handle keypresses in my application. When i add events to Keydown or Keypress to the main form,...
2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
3
by: Robert Inder | last post by:
I am struggling to catch kestrokes within an Internet Explorer 6 window. My window happens to be displaying three frames, though I suspect a similar problem would arise with a single document. ...
7
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.