Respond to keypresses on top of a paragraph

M

mflll

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
 
E

Evertjan.

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.
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top