Opera problem with onkeydown

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

    #1

    Opera problem with onkeydown

    I'm having trouble getting Opera to recognize a repeating key event - I was
    wondering if anyone had experience with this.

    Basically, I am trying to move a <div> based on the arrow keys that are
    pressed - left/right motion as you press the left & right arrow keys. I want
    the element to move as long as the key is held down.

    The code I'm using is:

    function movePaddle(e) {
    if (window.event) {
    key=event.keyCo de;
    } else {
    key = e.which;
    }
    if ((key == 37) && (padX > padMinX)) { padX-=5; }
    else if ((key == 39) && (padX < padMaxX)) { padX+=5; }
    document.getEle mentById("paddl e").style["left"]=padX;
    }

    And in the body:

    <body onKeyDown="java script:movePadd le(event);">

    This works for IE, Netscape, and Firefox, no problems. But Opera won't read
    the autorepeat of the key, so it only moves once each time you press the
    key.

    Any thoughts on a workaround for it?


  • Lasse Reichstein Nielsen

    #2
    Re: Opera problem with onkeydown

    "Tony" <someone@somewh ere.not> writes:
    [no autorepeat onkeydown event in Opera][color=blue]
    > Any thoughts on a workaround for it?[/color]

    Don't rely on autorepeat. Let it move from the first keydown to
    the first keyup, and ignore the keydowns in between.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Stephen Chalmers

      #3
      Re: Opera problem with onkeydown


      Tony <someone@somewh ere.not> wrote in message
      news:118hqac9lv nap3d@corp.supe rnews.com...[color=blue]
      > I'm having trouble getting Opera to recognize a repeating key event - I[/color]
      was[color=blue]
      > wondering if anyone had experience with this.
      >
      > Basically, I am trying to move a <div> based on the arrow keys that are
      > pressed - left/right motion as you press the left & right arrow keys. I[/color]
      want[color=blue]
      > the element to move as long as the key is held down.
      >
      > The code I'm using is:
      >
      >
      > This works for IE, Netscape, and Firefox, no problems. But Opera won't[/color]
      read[color=blue]
      > the autorepeat of the key, so it only moves once each time you press the
      > key.
      >
      > Any thoughts on a workaround for it?
      >
      >[/color]

      Set an interval when the key is pressed, and cancel it on release:

      var moveInterval=nu ll;

      function readArrow(e)
      {
      clearInterval( moveInterval );
      movePaddle( key=window.even t ? event.keyCode : e.which );
      moveInterval=se tInterval('move Paddle('+ key+' ) ', 100);
      }

      function movePaddle( key )
      {
      if ((key == 37) && (padX > padMinX)) { padX-=5; }
      else if ((key == 39) && (padX < padMaxX)) { padX+=5; }
      document.getEle mentById("paddl e").style["left"]=padX;
      }

      <body onKeyDown="move Paddle(event);" onkeyup='clearI nterval(moveInt erval)'>

      --
      Stephen Chalmers http://makeashorterlink.com/?H3E82245A
      547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134



      Comment

      • Tony

        #4
        Re: Opera problem with onkeydown

        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:64xj0xeo.f sf@hotpop.com.. .[color=blue]
        > "Tony" <someone@somewh ere.not> writes:
        > [no autorepeat onkeydown event in Opera][color=green]
        >> Any thoughts on a workaround for it?[/color]
        >
        > Don't rely on autorepeat. Let it move from the first keydown to
        > the first keyup, and ignore the keydowns in between.[/color]

        That was one of the possibilities I was considering - I'll have to play with
        it tonight


        Comment

        • Tony

          #5
          Re: Opera problem with onkeydown

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:64xj0xeo.f sf@hotpop.com.. .[color=blue]
          > "Tony" <someone@somewh ere.not> writes:
          > [no autorepeat onkeydown event in Opera][color=green]
          >> Any thoughts on a workaround for it?[/color]
          >
          > Don't rely on autorepeat. Let it move from the first keydown to
          > the first keyup, and ignore the keydowns in between.
          >[/color]

          It worked - thanks!

          One tweak was needed, though: the autorepeat caused the paddle to begin
          moving faster when it kicked in (bunches of keydown events firing off) - so
          I had to put a check to see if the keypress status was already set, before
          setting it again -

          But it works on Opera now, too.




          Comment

          Working...