Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

onkeypress event in <input /> tag

1 view
Skip to first unread message

Marco A. Cruz Quevedo

unread,
Sep 3, 2010, 12:23:28 PM9/3/10
to
Hi everybody,

I have the following:

. . . . . . . . . . . . . . . . . .

<input type="text" name="capt_var" value=" " maxlength="10"
onkeypress="keystroke()" />

. . . . . . . . . . . . . . . . . .

The onkeypress clause calls keystroke function but, inside it, how can
I retrieve the last key pressed, before it is appended to the variable
being captured?

Thanks in advance for your help.

Regards,

Marco

SAM

unread,
Sep 3, 2010, 1:00:10 PM9/3/10
to
Le 03/09/10 18:23, Marco A. Cruz Quevedo a écrit :

> Hi everybody,
>
> I have the following:
>
> . . . . . . . . . . . . . . . . . .
>
> <input type="text" name="capt_var" value=" " maxlength="10"
> onkeypress="keystroke()" />
>
> . . . . . . . . . . . . . . . . . .
>
> The onkeypress clause calls keystroke function but, inside it, how can
> I retrieve the last key pressed, before it is appended to the variable
> being captured?

take care that keypress isn't keydown
(think that on key pressing the key isn't yet known)

see back the post "onkeypress" some hours ago (yesterday and younger)

<http://unixpapa.com/js/key.html>
<http://www.cinsoft.net/keyboard.html>

--
Stéphane Moriaux avec/with iMac-intel

David Mark

unread,
Sep 3, 2010, 7:06:15 PM9/3/10
to
On Sep 3, 1:00 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:

> Le 03/09/10 18:23, Marco A. Cruz Quevedo a écrit :
>
> > Hi everybody,
>
> > I have the following:
>
> >   . . . . . . . . . . . . . . . . . .
>
> > <input type="text" name="capt_var" value="    "  maxlength="10"
> > onkeypress="keystroke()" />
>
> >   . . . . . . . . . . . . . . . . . .
>
> > The onkeypress clause calls keystroke function but, inside it, how can
> > I retrieve the last key pressed, before it is appended to the variable
> > being captured?
>
> take care that keypress isn't keydown
> (think that on key pressing the key isn't yet known)

The key code is known on keydown. The key code and character code are
both known on keypress.

Ry Nohryb

unread,
Sep 4, 2010, 4:25:59 AM9/4/10
to
On Sep 3, 7:00 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
> (...)
> <http://unixpapa.com/js/key.html>
> (...)

@ http://unixpapa.com/js/ :

<quote>
"Internet Explorer
Seriously old fashioned and hardly compliant to standards. Microsoft,
out of selfless devotion to backwards compatibility, trained IE 6 and
7 to lovingly emulate all the miserable bugs from IE 5, unless the web
page author begged them not to, in which case they run in a slightly
less buggy mode. With the recent IE 8 release, they fixed enough bugs
and made enough other improvements to close about half the distance
between them and all the good browsers, so they introduced yet another
backwards compatibility mode, so it can now emulate either IE 5 bugs
or IE 7 bugs. Of course, there are bugs in these bug emulations, where
some bugs don't work exactly the same way they did in the original
buggy browser, but the IE developers are working themselves buggy to
fix all these buggy bugs. With so much time devoted to making their
browser work wrong, it's hardly surprising that they can't also find
the time to make it work right. So it's left to the world's web
developers to work around all of IE's bugs so that IE users can
persist in the illusion that they are using a functional web browser.
I guess we must all love Microsoft, or we wouldn't do it."
</quote>

:-))
--
Jorge.

Thomas 'PointedEars' Lahn

unread,
Sep 3, 2010, 5:33:05 PM9/3/10
to
Marco A. Cruz Quevedo wrote:

> I have the following:
>
> . . . . . . . . . . . . . . . . . .
>
> <input type="text"

^^^^^^^^^^^
Superfluous, it is the default.

> name="capt_var"

Dump the idea of having so-called CAPTCHAs; they are essentially
an accessibility nightmare, and can be easily circumvented:
<http://en.wikipedia.org/wiki/CAPTCHA>

> value=" " maxlength="10"
^^^^
Pardon?

> onkeypress="keystroke()" />

Why XHTML?



> . . . . . . . . . . . . . . . . . .
>
> The onkeypress clause

It is an event-handler attribute value, not a "clause".

> calls keystroke function but, inside it, how can
> I retrieve the last key pressed, before it is appended to the variable
> being captured?

One or more of event.keyCode, event.which, or event.charCode would come in
handy. `event' is proprietarily but (AFAIK) uniformly implemented for
event-handler attribute values in (X)HTML DOMs, so you can pass this value
and use the object reference in the function. You will notice that
`onkeypress', `onkeydown' and `onkeyup' are suited to retrieve information
about different types of keypresses each.

David Mark has recently posted yet another keyboard events test case here
which might be of use to you (I did not have time to have a look at it yet).
I have found <http://www.brain4.de/programmierecke/js/tastatur.php#start>
useful (both the form and the code behind it).


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300...@news.demon.co.uk> (2004)

0 new messages