Capture keypress inside a textarea

H

Hans

How do I capture the keycode inside a <textarea> in NN6+ and IE5.5+? I don't
want to add add document.keyPress function because I only want this to
occure when a specific textarea is selected and not fire for every keypress
in the entire document.

<textarea onkeypress="captureKeys();"....

but I cannot get a reference to the current event so i can ask for the
keyCode. In IE I can check the event.keyCode inside my function captureKeys
but this is not possible in NN6+.

Regards
/Stefan
 
M

Martin Honnen

Hans wrote:

<textarea onkeypress="captureKeys();"....

but I cannot get a reference to the current event so i can ask for the
keyCode. In IE I can check the event.keyCode inside my function captureKeys
but this is not possible in NN6+.

Simply use
<textarea onkeypress="return captureKeys(event);"
and then
function captureKeys (evt) {
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode == ...) {
// cancel key:
if (evt.preventDefault) {
evt.preventDefault();
}
return false;
}
return true;
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top