KeyPress Events in IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Brooks

    KeyPress Events in IE

    Is there any way in JavaScript to determine which key has been pressed
    during any of the key-press, key-down, key-up events in IE6+? It
    appears the "which" property only works for Netscape.
  • Jules

    #2
    Re: KeyPress Events in IE

    I can help you with this!

    In netscape, it is evt.which. In IE, it's event.keyCode. Here's a snippet
    of code I use:

    function onlyNumbersXB(o bj, e, mask) {

    var keyCode;
    var returnVal = false;

    if (window.event) {
    e = window.event;
    keyCode = e.keyCode;
    } else {
    // a key, such as delete, was pressed; let it pass through
    if (e.keyCode == e.which) keyCode = null;
    // else if not a char we don't know what this is; let it pass through
    else if (e.charCode != e.which) keyCode = void 0;
    // else a char - set it
    else keyCode = e.which;
    }

    // do whatever you want based on keyCode...

    // now for the return
    if (window.event) {
    e.returnValue = returnVal;
    return returnVal; // not really necessary but I'm trying to get
    Safari to work
    } else
    return returnVal;
    }

    }

    This method is invoked on a text field, onKeyPress='ret urn
    onlyNumbersXB(t his, event, mask)'

    My problem, as posted elsewhere, is getting Safari to behave itself. It has
    window.event and behaves that way. It sets e.returnValue appropriately. It
    still doesn't recognize "false" and let's the key pass.

    Anyone?

    Julia

    "Ron Brooks" <ronbrooks@dend ress.com> wrote in message
    news:25459614.0 310071743.495ac 0cc@posting.goo gle.com...[color=blue]
    > Is there any way in JavaScript to determine which key has been pressed
    > during any of the key-press, key-down, key-up events in IE6+? It
    > appears the "which" property only works for Netscape.[/color]


    Comment

    • Richard Cornford

      #3
      Re: KeyPress Events in IE

      "Jules" <jules9514@yaho o.com> wrote in message
      news:127051ac07 a23678f1365bca2 0fde760@news.te ranews.com...
      <snip>[color=blue]
      >My problem, as posted elsewhere, is getting Safari to behave itself.
      >It has window.event and behaves that way. It sets e.returnValue
      >appropriatel y. It still doesn't recognize "false" and let's the
      >key pass.[/color]

      Did you try the W3C DOM events - preventDefault - method, combined with
      checking that the event is cancelable by checking the event's W3C DOM
      (boolean) - cancelable - property? Also (assuming that is no-go) have
      you considered trying to cancel onkeydown and/or onkeyup instead of (or
      in addition to) onkeypress?

      Richard.


      Comment

      • Jules

        #4
        Re: KeyPress Events in IE


        "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
        news:bm05p7$id5 $1$8302bc10@new s.demon.co.uk.. .[color=blue]
        > "Jules" <jules9514@yaho o.com> wrote in message
        > news:127051ac07 a23678f1365bca2 0fde760@news.te ranews.com...
        > <snip>[color=green]
        > >My problem, as posted elsewhere, is getting Safari to behave itself.
        > >It has window.event and behaves that way. It sets e.returnValue
        > >appropriatel y. It still doesn't recognize "false" and let's the
        > >key pass.[/color]
        >
        > Did you try the W3C DOM events - preventDefault - method, combined with
        > checking that the event is cancelable by checking the event's W3C DOM
        > (boolean) - cancelable - property?[/color]

        I did try preventDefault( ) -- no go. I don't know what you mean by
        "combined with if the event is cancelable". What would combining the two
        do?

        [color=blue]
        >Also (assuming that is no-go) have
        > you considered trying to cancel onkeydown and/or onkeyup instead of (or
        > in addition to) onkeypress?[/color]

        Yea - except we have problems with onKeyDown and Netscape 7. That's why we
        went with onKeyPress.

        Canceling keystrokes is something that whole documents have been written
        about regarding IE and Netscape. I can't believe that I can be the only
        person struggling with this with Safari...

        Julia


        Comment

        • Richard Cornford

          #5
          Re: KeyPress Events in IE

          "Jules" <jules9514@yaho o.com> wrote in message
          news:92f68b02b1 3a2f8a90f42ef60 59c19ec@news.te ranews.com...
          <snip>[color=blue]
          >I did try preventDefault( ) -- no go. I don't know what you
          >mean by "combined with if the event is cancelable". What
          >would combining the two do?[/color]

          I mean that if the event object has the W3C DOM events specified
          boolean - cancelable - property and it is false then there is not much
          point in calling its - preventDefault - method as the event cannot be
          cancelled.
          [color=blue][color=green]
          >>Also (assuming that is no-go) have you considered trying to
          >>cancel onkeydown and/or onkeyup instead of (or
          >>in addition to) onkeypress?[/color]
          >
          >Yea - except we have problems with onKeyDown and Netscape 7.
          >That's why we went with onKeyPress.[/color]
          [color=blue]
          >Canceling keystrokes is something that whole documents have
          >been written about regarding IE and Netscape. I can't
          >believe that I can be the only person struggling with this
          >with Safari...[/color]

          Validating user input as they type is problematic at best, clear
          instructions often make it unnecessary and you usually won't catch paste
          operations so the field would still need to be validated onsubmit
          anyway. These days I probably would not bother so I do not know any
          currently recommended method.

          However, though I don't have access to a Mac at present so I cannot test
          Safari, I would expect Konqueror (on which Safari is based) to exhibit
          the same problem, so if you could make up a minimal test case page that
          does exactly what you require where it is working now (and preferably
          nothing else) and post it (or a URL to an online version) then I (and/or
          someone else) would probably have a look at it and see if it cannot be
          made to do what you want.
          [color=blue]
          >Julia[/color]

          That is not a name you see often these days. You share it with my sister
          :)

          Richard.


          Comment

          Working...