Connecting Tech Pros Worldwide Help | Site Map

Opera problem with onkeydown

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 07:50 PM
Tony
Guest
 
Posts: n/a
Default 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.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?



  #2  
Old July 23rd, 2005, 07:50 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default 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.'
  #3  
Old July 23rd, 2005, 07:51 PM
Stephen Chalmers
Guest
 
Posts: n/a
Default 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



  #4  
Old July 23rd, 2005, 07:51 PM
Tony
Guest
 
Posts: n/a
Default 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


  #5  
Old July 23rd, 2005, 07:54 PM
Tony
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.