onKeyDown, getting rid of keys typed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew DeFaria

    onKeyDown, getting rid of keys typed

    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.

  • Stephen Chalmers

    #2
    Re: onKeyDown, getting rid of keys typed


    "Andrew DeFaria" <Andrew@DeFaria .com> wrote in message
    news:c8554$40a2 3d55$43661972$3 0587@msgid.mega newsservers.com ...[color=blue]
    > 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![/color]

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



    Comment

    • Andrew DeFaria

      #3
      Re: onKeyDown, getting rid of keys typed

      "Stephen Chalmers" <spamTheMothers ToBuggery@ntlwo rld.com> wrote in message
      news:8htoc.100$ R3.83@newsfe1-win...[color=blue]
      >
      > "Andrew DeFaria" <Andrew@DeFaria .com> wrote in message
      > news:c8554$40a2 3d55$43661972$3 0587@msgid.mega newsservers.com ...[color=green]
      > > 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![/color]
      >
      > Use the onkeyup event instead; that way the 't' is overwritten by the date
      > insertion.[/color]

      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.


      Comment

      • Stephen Chalmers

        #4
        Re: onKeyDown, getting rid of keys typed


        "Andrew DeFaria" <Andrew@DeFaria .com> wrote in message
        news:170b1$40a2 a587$c09cfc9$26 16@msgid.megane wsservers.com.. .[color=blue]
        > "Stephen Chalmers" <spamTheMothers ToBuggery@ntlwo rld.com> wrote in message
        > news:8htoc.100$ R3.83@newsfe1-win...[color=green]
        > >
        > > "Andrew DeFaria" <Andrew@DeFaria .com> wrote in message
        > > news:c8554$40a2 3d55$43661972$3 0587@msgid.mega newsservers.com ...[color=darkred]
        > > > 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 -[/color][/color][/color]
        I[color=blue][color=green][color=darkred]
        > > > 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![/color]
        > >
        > > Use the onkeyup event instead; that way the 't' is overwritten by the[/color][/color]
        date[color=blue][color=green]
        > > insertion.[/color]
        >
        > 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?[/color]

        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.

        --


        Comment

        • Andrew DeFaria

          #5
          Re: onKeyDown, getting rid of keys typed

          Stephen Chalmers wrote:
          [color=blue]
          > 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.[/color]

          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.
          [color=blue]
          > 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.[/color]

          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?

          Comment

          Working...