keydown listener for div element and "event forwarding"

G

Gregor Kofler

I'm trying to "detect" a keydown event in a DIV.

The idea have a keydown listener attached to document and forward the
event to the div. (I've searched the web, but it was impossible to
handle the noise on this topic.)

I've tried a simple prototype in Firefox and it seems to work.

function forward(e, elem) {
var f, p;
f = document.createEvent("Event");
f.initEvent(e.type, true, true);
for(p in e) {
if(typeof f[p] === "undefined") {
f[p] = e[p];
}
}
elem.dispatchEvent(f);
}

Three questions remain:
(a) is this whole approach crap?

(b) can keydown trigger a listener attached to the div directly (quite a
few oldish sources on google say so, I couldn't confirm that, and the
official mozilla documentation is rather sparse on that)?

(c) did I overlook something serious in my forward function?

Gregor
 
R

RobG

Gregor said:
I'm trying to "detect" a keydown event in a DIV.

The idea have a keydown listener attached to document and forward the
event to the div. (I've searched the web, but it was impossible to
handle the noise on this topic.)

I've tried a simple prototype in Firefox and it seems to work.

function forward(e, elem) {
var f, p;
f = document.createEvent("Event");
f.initEvent(e.type, true, true);
for(p in e) {
if(typeof f[p] === "undefined") {
f[p] = e[p];
}
}
elem.dispatchEvent(f);
}

Three questions remain:
(a) is this whole approach crap?

Dunno, createEvent isn't supported by at least one popular browser so if
you care about 60~80% of web traffic, it might be. Also, there are
vaguaries with calling createEvent - even different versions of Mozilla
behave differently. So don't use it unless you are targetting a
specific browser that supports it.

(b) can keydown trigger a listener attached to the div directly (quite a
few oldish sources on google say so, I couldn't confirm that, and the
official mozilla documentation is rather sparse on that)?

Mozilla? Hardly an authority on HTML - the W3C HTML 4.01 is:


<UR: http://www.w3.org/TR/html4/struct/global.html#edef-DIV >


It says the div element supports onkeydown, so you should have no
problems in conforming browsers.

(c) did I overlook something serious in my forward function?

That it's not necessary? :)
 
G

Gregor Kofler

RobG meinte:
Dunno, createEvent isn't supported by at least one popular browser so if
you care about 60~80% of web traffic, it might be. Also, there are
vaguaries with calling createEvent - even different versions of Mozilla
behave differently. So don't use it unless you are targetting a
specific browser that supports it.

I know. It's a "prototype". My library handles the different models.
<UR: http://www.w3.org/TR/html4/struct/global.html#edef-DIV >

It says the div element supports onkeydown, so you should have no
problems in conforming browsers.

Sort of. If you fire a "keydown" onto the div or one comes bubbling
along from an input, it works. But just
clicking-on-a-div-and-hitting-a-key doesn't evoke any reaction in my
standard keydown listener.
That it's not necessary? :)

Nice try. ;-) I'd be happier without it, too. Though I'm not convinced
that I can solve it so easily.

Gregor
 
M

Martin Honnen

Gregor said:
I'm trying to "detect" a keydown event in a DIV.

(b) can keydown trigger a listener attached to the div directly

Yes, give the div a tabindex="0" and it becomes focussable and can
receive key events in IE, Mozilla, Opera.
 
G

Gregor Kofler

Martin Honnen meinte:
Yes, give the div a tabindex="0" and it becomes focussable and can
receive key events in IE, Mozilla, Opera.

Thanks. That's what I'd been looking for. Is there any documentation for
this ..er... bizarre workaround?

Gregor
 
D

dhtml

Gregor said:
Martin Honnen meinte:

Thanks. That's what I'd been looking for. Is there any documentation for
this ..er... bizarre workaround?

Yes on MSDN and developer.mozilla.org.

The one problem I've found is that I couldn't can't get the onclick to
fire on Enter press on an arbitrary element.

Garrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top