onKeyDown, getting rid of keys typed

A

Andrew DeFaria

I'm trying to use the onKeyDown event on a text field. I intersept the
keystroke, interpret it and then do certain actions based on certain
keys stuck. If a special key is stuck I replace the value in the text
field and return false. This seems to say "get rid of the key struck - I
handled it instead" for IE but not for Netscape.

I set up a page, http://defaria.com/test.html to demonstrate this. If
you type any character it is simply inserted. If you type a "t",
however, the field is replaced with the current date. In IE this works
just fine. In Netscape I get the current date followed by the "t"
character! Trying to debug this I inserted an alert into the code. The
strange thing is that with the alert Netscape stops appending the "t"!
This is demonstrated by http://defaria.com/test.html under Netscape
(actually I'm using Firefox). The first box, labelled SetToday1, will
pop up an alert box for any key you type. Again if you type "t" the
current date is inserted and "t" is *not* appended to the date inserted.

The second box, labelled SetToday2, will not pop up an alert box but it
functions similarly in that typing a "t" will insert the current date.
However, SetToday2, without the call to alert, will append a "t" to the
date inserted even though false is being returned. This happens only on
Netscape and not on IE. The question is why?

(To view the source use *View: Source*)
 
A

Andrew DeFaria

Stephen Chalmers said:
Use the onkeyup event instead; that way the 't' is overwritten by the date
insertion.

Huh? Use onKeyup event to do what? IOW what do I do in my onKeyup event
handler to get rid of the "t"? Merely set textbox.value to the correct
value?

Besides this won't work for me. Although my example was merely of using "t"
to get the current date inserted, thus probably only struck once, I want to
then allow "+" to advance one day. Well advancing one day is also a single
keystroke but I want to be able to hold down the "+" key to "scroll" forward
in dates. If I am using onKeyup then this would look real odd as many "+"'s
get inserted, in the process scrolling the date out of sight, only to be
"corrected" when the user finally releases the "+" key.
 
S

Stephen Chalmers

Andrew DeFaria said:
Huh? Use onKeyup event to do what? IOW what do I do in my onKeyup event
handler to get rid of the "t"? Merely set textbox.value to the correct
value?

My response to the problem you mentioned originally meant that if you
trigger your setToday#
functions with onkeyup instead, the 't' appears only momentarily before
being overwritten by
the date. I tried it on your code under Mozilla and it worked.
If now you say you want to do other things with keystrokes, then that's
another matter.
I suggest you look into the use of arrow keys.

--
 
A

Andrew DeFaria

Stephen said:
My response to the problem you mentioned originally meant that if you
trigger your setToday# functions with onkeyup instead, the 't' appears
only momentarily before being overwritten by the date. I tried it on
your code under Mozilla and it worked.

As I said, that won't work if you want the action to happen on the
downstroke and/or you want the action to be repeated. Using onKeyUp
would require that the user repetitively strike the key each time in
order to generate a KeyUp event.
If now you say you want to do other things with keystrokes, then
that's another matter. I suggest you look into the use of arrow keys.

Of course I want to do other things! I just broke the problem down to
simplify it here. What I'm hoping to eventually do is emulate the
behavior in Quicken where "t" goes to today and "+" increases the date
and "-" decreases it.

I've improved my test. It now responds to "t" and +/- properly. I had to
implement a onKeyUp event to fix up the date by removing the +/- after
the user releases the key. But under Netscape it just looks, well,
unprofessional! What is the defined behavior supposed to be when the
function returns false? IE takes it as "forget that character". Netscape
doesn't. And the onKeyUp that fixes the date is a hack at best.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top