468,554 Members | 2,003 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

Respond to keypresses on top of a paragraph

I am having trouble responding writing a Javascript Program respond
to keypresses on top of a paragraph. Here is the code I tried:

<HTML>
<SCRIPT Language="JavaScript">
document.write("TEeve");
function QQ(event){
alert("Key Pressed " + event.charCode());
}

</SCRIPT>

<P onkeypress="QQ(event)">Testing</P>

The alert is not displayed when I click on the paragraph and press
ordinary
keys.

By the way the following related Javascript to respond to mouse events
on a
paragraph does work.

<HTML>
<SCRIPT Language="JavaScript">
document.write("Tex");
function QQ(event){
alert("Displaying Mouse Coordiantes "+event.clientX
+","+event.clientY);
}

</SCRIPT>
<body>
<P onmousedown="QQ(event)">Testing</P>
</body>
</html>

Thus, what don't I understand about the keypressed event?
I am running Mozilla 5.0 (rv:1.7.10 Geck/200507

I also tried someother options suggested by the W3C Level Tw Events
Specification.

But section 1.6.3 said that "the DOM Level 2 Event specification does
not provid
a key event module."
Dr. Leff, Western Illinois University FAX 309 298 2302 Pager 309 367
0787
Associate Professor of Computer Science, One University Circle, Macomb
IL 61455

Apr 14 '07 #1
1 1499
wrote on 14 apr 2007 in comp.lang.javascript:
I am having trouble responding writing a Javascript Program respond
to keypresses on top of a paragraph. Here is the code I tried:

<HTML>
<SCRIPT Language="JavaScript">
document.write("TEeve");
function QQ(event){
alert("Key Pressed " + event.charCode());
}

</SCRIPT>

<P onkeypress="QQ(event)">Testing</P>

The alert is not displayed when I click on the paragraph and press
ordinary keys.
You cannot key anything on an element that cannot receive KEY presses.
Such element is not even "on focus" for the keyboard.
By the way the following related Javascript to respond to mouse events
on a
paragraph does work.

<HTML>
<SCRIPT Language="JavaScript">
document.write("Tex");
function QQ(event){
alert("Displaying Mouse Coordiantes "+event.clientX
+","+event.clientY);
}

</SCRIPT>
<body>
<P onmousedown="QQ(event)">Testing</P>
</body>
</html>
However you can get the mouse(!) coordinates of a MOUSE action.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ian Henderson | last post: by
9 posts views Thread by robert_rowe | last post: by
1 post views Thread by Jean-François Michaud | last post: by
3 posts views Thread by gentsquash | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.