By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Respond to keypresses on top of a paragraph

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.