Connecting Tech Pros Worldwide Help | Site Map

Opera problem with onkeydown

Tony
Guest
 
Posts: n/a
#1: Jul 23 '05
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.keyCode;
} else {
key = e.which;
}
if ((key == 37) && (padX > padMinX)) { padX-=5; }
else if ((key == 39) && (padX < padMaxX)) { padX+=5; }
document.getElementById("paddle").style["left"]=padX;
}

And in the body:

<body onKeyDown="javascript:movePaddle(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
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Opera problem with onkeydown


"Tony" <someone@somewhere.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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Stephen Chalmers
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Opera problem with onkeydown



Tony <someone@somewhere.not> wrote in message
news:118hqac9lvnap3d@corp.supernews.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=null;

function readArrow(e)
{
clearInterval( moveInterval );
movePaddle( key=window.event ? event.keyCode : e.which );
moveInterval=setInterval('movePaddle('+ key+' ) ', 100);
}

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

<body onKeyDown="movePaddle(event);" onkeyup='clearInterval(moveInterval)'>

--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134



Tony
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Opera problem with onkeydown


"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:64xj0xeo.fsf@hotpop.com...[color=blue]
> "Tony" <someone@somewhere.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


Tony
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Opera problem with onkeydown


"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:64xj0xeo.fsf@hotpop.com...[color=blue]
> "Tony" <someone@somewhere.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.

http://www.dslextreme.com/~tony23/bricks.htm


Closed Thread