keydown listener for div element and "event forwarding"

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

    #1

    keydown listener for div element and "event forwarding"

    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.create Event("Event");
    f.initEvent(e.t ype, true, true);
    for(p in e) {
    if(typeof f[p] === "undefined" ) {
    f[p] = e[p];
    }
    }
    elem.dispatchEv ent(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
  • RobG

    #2
    Re: keydown listener for div element and "event forwarding&quot ;

    Gregor Kofler wrote:
    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.create Event("Event");
    f.initEvent(e.t ype, true, true);
    for(p in e) {
    if(typeof f[p] === "undefined" ) {
    f[p] = e[p];
    }
    }
    elem.dispatchEv ent(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? :-)


    --
    Rob

    Comment

    • Gregor Kofler

      #3
      Re: keydown listener for div element and &quot;event forwarding&quot ;

      RobG meinte:
      >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.
      I know. It's a "prototype" . My library handles the different models.
      >(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)?
      <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.
      >(c) did I overlook something serious in my forward function?
      >
      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

      Comment

      • Martin Honnen

        #4
        Re: keydown listener for div element and &quot;event forwarding&quot ;

        Gregor Kofler wrote:
        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.


        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Gregor Kofler

          #5
          Re: keydown listener for div element and &quot;event forwarding&quot ;

          Martin Honnen meinte:
          Gregor Kofler wrote:
          >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.
          Thanks. That's what I'd been looking for. Is there any documentation for
          this ..er... bizarre workaround?

          Gregor

          Comment

          • Martin Honnen

            #6
            Re: keydown listener for div element and &quot;event forwarding&quot ;

            Gregor Kofler wrote:
            Thanks. That's what I'd been looking for. Is there any documentation for
            this ..er... bizarre workaround?
            I am sure MSDN documents that for IE, other browsers followed and I
            think HTML 5 respectively whatwg documents (or rather specifies) that
            somewhere: http://www.whatwg.org/specs/web-apps...nt-work/#focus

            --

            Martin Honnen
            http://JavaScript.FAQTs.com/

            Comment

            • dhtml

              #7
              Re: keydown listener for div element and &quot;event forwarding&quot ;

              Gregor Kofler wrote:
              Martin Honnen meinte:
              >Gregor Kofler wrote:
              >>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.
              >
              Thanks. That's what I'd been looking for. Is there any documentation for
              this ..er... bizarre workaround?
              >
              Yes on MSDN and developer.mozil la.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
              Gregor

              --
              comp.lang.javas cript FAQ <URL: http://jibbering.com/faq/ >

              Comment

              Working...