Javascript Cancel OnKeyPress prob on Safari

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Brown

    #1

    Javascript Cancel OnKeyPress prob on Safari

    Hi,
    I want to cancel a certain key in JavaScript.
    To do so, I catch the event in OnKeyPress and cancel the default:

    <head>
    ....
    function f(evt)
    {
    var evt = (evt) ? evt : ((window.event) ? window.event : "")
    ...
    evt.returnValue = false; // IE
    evt.preventDefa ult(); // dom 2 (also tried without this here)
    return false; // netscape
    }
    ....
    <body>
    ....
    <TEXTAREA Name=demoTextar ea ROWS=10 COLS=50 OnKeyPress="f(e vent)">
    </TEXTAREA>

    IE works fine.
    Safari will not cancel the key.
    I also checked to see if the textArea value already had received the
    key before this handler. It had not.

    Any ideas?

    Thanks in advance,
    Fred Brown
  • BreadWithSpam@fractious.net

    #2
    Re: Javascript Cancel OnKeyPress prob on Safari

    fredb@inter.net .il (Fred Brown) writes:

    [color=blue]
    > I want to cancel a certain key in JavaScript.[/color]

    Why?

    I really hate it when folks use JavaScript to screw up
    what I expect my browser to do.

    I really can't think of a valid reason for you to be able to
    do this. Just don't.


    --
    Plain Bread alone for e-mail, thanks. The rest gets trashed.
    No HTML in E-Mail! -- http://www.expita.com/nomime.html
    Are you posting responses that are easy for others to follow?

    Comment

    • Fred Brown

      #3
      Re: Javascript Cancel OnKeyPress prob on Safari

      BreadWithSpam@f ractious.net wrote in message news:<yobhe41z5 1u.fsf@panix2.p anix.com>...[color=blue]
      > fredb@inter.net .il (Fred Brown) writes:
      >
      >[color=green]
      > > I want to cancel a certain key in JavaScript.[/color]
      >
      > Why?[/color]
      I did want to waste the forum's bandwidth with unnecessary details of
      my app,
      but in short, I am not actually just canceling the key, I am replacing
      the
      inputted keys with other keys. This makes sense in my app. It is the
      reason that this textarea exists. My problem is that both the
      replaced key and the original one end up in the textarea on Safari.
      Does anybody know how to achieve this replacement?
      [color=blue]
      >
      > I really hate it when folks use JavaScript to screw up
      > what I expect my browser to do.
      >
      > I really can't think of a valid reason for you to be able to
      > do this. Just don't.[/color]

      Comment

      • Peter Hurford

        #4
        Re: Javascript Cancel OnKeyPress prob on Safari

        I can't remember the exact details but I seem to recall having problems
        trapping keys - esp. the tab key - with onkeypress.

        My solution was to look at onkeydown instead.

        I can think of lots of reasons why you'd want to do this, by the way.

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Fred Brown

          #5
          Re: Javascript Cancel OnKeyPress prob on Safari

          Peter,
          I checked if I could cancel the onKeyDown on Safari and I could not.
          I am no JavaScript expert but I think that onKeyPress is better for me
          because it has already translated from virtual key to unicode. Also,
          I believe if you hold down a key, you will get many onKeyPress events
          for each
          char that is processed but only one keyDown. I think you are right
          though
          that keyPress does not get called for all keys - I saw that it did not
          get called on IE for a backspace (which makes sense since I assume
          there is no
          unicode defined for keys like a backspace.)

          Thanks anyhow,
          Fred Brown

          Peter Hurford <peter.hurford@ microcrest.com> wrote in message news:<3f5353d0$ 0$62081$7586835 5@news.frii.net >...[color=blue]
          > I can't remember the exact details but I seem to recall having problems
          > trapping keys - esp. the tab key - with onkeypress.
          >
          > My solution was to look at onkeydown instead.
          >
          > I can think of lots of reasons why you'd want to do this, by the way.
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]

          Comment

          • Jules

            #6
            Re: Javascript Cancel OnKeyPress prob on Safari

            Hi Fred,

            Did you ever find a solution for this? I am now encountering the same
            problem. We want to cancel non-numeric characters and have found a way to
            do it that is supported by both IE6 and NS7 - onKeyPress event with a
            handler that returns false (or sets event.returnVal ) if not numeric. Now
            I'm trying to get it to work with Safari and it just won't. The onKeyPress
            event fires, the handler is called, returns false appropriately, but doesn't
            cancel the key. Since it has a window.event like IE, I tried setting
            returnVal to false - nada. I tried setting event.keyCode to something
            else - nada. I tried overriding the value of the object - nada.

            Anyone have any ideas? Your help is greatly appreciated!

            Julia Allen
            juliaallen@delt ek.com


            "Fred Brown" <fredb@inter.ne t.il> wrote in message
            news:4536eeaf.0 309020523.50f58 ff7@posting.goo gle.com...[color=blue]
            > Peter,
            > I checked if I could cancel the onKeyDown on Safari and I could not.
            > I am no JavaScript expert but I think that onKeyPress is better for me
            > because it has already translated from virtual key to unicode. Also,
            > I believe if you hold down a key, you will get many onKeyPress events
            > for each
            > char that is processed but only one keyDown. I think you are right
            > though
            > that keyPress does not get called for all keys - I saw that it did not
            > get called on IE for a backspace (which makes sense since I assume
            > there is no
            > unicode defined for keys like a backspace.)
            >
            > Thanks anyhow,
            > Fred Brown
            >
            > Peter Hurford <peter.hurford@ microcrest.com> wrote in message[/color]
            news:<3f5353d0$ 0$62081$7586835 5@news.frii.net >...[color=blue][color=green]
            > > I can't remember the exact details but I seem to recall having problems
            > > trapping keys - esp. the tab key - with onkeypress.
            > >
            > > My solution was to look at onkeydown instead.
            > >
            > > I can think of lots of reasons why you'd want to do this, by the way.
            > >
            > > *** Sent via Developersdex http://www.developersdex.com ***
            > > Don't just participate in USENET...get rewarded for it![/color][/color]


            Comment

            Working...