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

onKeyDown, getting rid of keys typed

P: n/a
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*)
--
When something is "new and improved!". Which is it? If it's new, then
there has never been anything before it. If it's an improvement, then
there must have been something before it.

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Andrew DeFaria" <An****@DeFaria.com> wrote in message
news:c8***************************@msgid.meganewss ervers.com...
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!


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

Jul 23 '05 #2

P: n/a
"Stephen Chalmers" <sp*********************@ntlworld.com> wrote in message
news:8htoc.100$R3.83@newsfe1-win...

"Andrew DeFaria" <An****@DeFaria.com> wrote in message
news:c8***************************@msgid.meganewss ervers.com...
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!


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.
Jul 23 '05 #3

P: n/a

"Andrew DeFaria" <An****@DeFaria.com> wrote in message
news:17*************************@msgid.meganewsser vers.com...
"Stephen Chalmers" <sp*********************@ntlworld.com> wrote in message
news:8htoc.100$R3.83@newsfe1-win...

"Andrew DeFaria" <An****@DeFaria.com> wrote in message
news:c8***************************@msgid.meganewss ervers.com...
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!


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?


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.

--
Jul 23 '05 #4

P: n/a
Stephen Chalmers wrote:
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.
--
Ever stop to think, and forget to start again?
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.