programmatically generated keyboard events initKeyEvent

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

    #1

    programmatically generated keyboard events initKeyEvent


    How to use generated keyboard events?

    What I am trying here to do is in onkeyup event handler



    generate a (shift)TAB keydown so the focus would move to the
    (previous/next ) input element.

    --
    marekmand
  • Michael Winter

    #2
    Re: programmaticall y generated keyboard events initKeyEvent

    On Wed, 03 Mar 2004 16:00:09 +0200, Marek Mand <cador.soft@mai l.ee> wrote:
    [color=blue]
    > How to use generated keyboard events?[/color]

    At this time, it is not possible (as far as I know) to emulate keyboard
    events. In a later version of the Document Object Model (DOM) Event
    specification[1], key events will be introduced. Even so, it will probably
    be a while before browsers implement the new features. As Netscape
    Communications Corporation have reportedly stopped development of the
    Netscape browser, it is unlikely that it will ever support the new DOM
    version.
    [color=blue]
    > What I am trying here to do is in onkeyup event handler
    >
    > http://www.hot.ee/idaliiga/braggart/createEventTest.htm
    >
    > generate a (shift)TAB keydown so the focus would move to the
    > (previous/next ) input element.[/color]

    What you should be able to do here is detect the space and backspace key
    presses, and call the focus() method on the next or previous control,
    respectively. There is no actual need to simulate a tab press.

    Mike


    [1] I refer to DOM Level 3 Events, which has not yet become a W3C
    Recommendation. Level 2 is the current version.

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Marek Mand

      #3
      Re: programmaticall y generated keyboard events initKeyEvent

      Michael Winter wrote:[color=blue]
      > On Wed, 03 Mar 2004 16:00:09 +0200, Marek Mand <cador.soft@mai l.ee> wrote:[color=green]
      >> How to use generated keyboard events?[/color][/color]
      [color=blue]
      > At this time, it is not possible (as far as I know) to emulate keyboard
      > events.[/color]


      Is that person then delerious there??

      [color=blue]
      >In a later version of the Document Object Model (DOM) Event
      > specification[1], key events will be introduced.[/color]

      I read that too, but whats the point of mentioning that in the
      documentation in first place... ;D
      [color=blue]
      > Even so, it will probably be a while before browsers implement the new features.[/color]

      Grins yea I wait the time ehen I can create mouse event mousemove
      programmaticall y =D grins...

      [color=blue][color=green]
      >> What I am trying here to do is in onkeyup event handler
      >> http://www.hot.ee/idaliiga/braggart/createEventTest.htm
      >> generate a (shift)TAB keydown so the focus would move to the
      >> (previous/next ) input element.[/color]
      > There is no actual need to simulate a tab press.[/color]

      I think it is.
      How else would I know which is the previous or next form control ?

      A form control doesnt have properties like
      previousFormCon trol nextFormControl

      The whole point of why I wanted to 'stuff the keyboard buffer' with tab
      char is that I wanted to let the browser do the underlying work of
      moving the input focus and not me.

      If I have no fixed ID attributes and controller object that descibes the
      relation and order of references of input elements how would I know
      which is previous and which is next?

      I dont think that as a ugly hack walking the tree and making descisions
      upon it guarantees me the "natural order of form elements navigation".

      So what are the options?




      Comment

      • Michael Winter

        #4
        Re: programmaticall y generated keyboard events initKeyEvent

        On Wed, 03 Mar 2004 17:22:34 +0200, Marek Mand <cador.soft@mai l.ee> wrote:

        [a person using initKeyEvent()]
        [color=blue]
        > Is that person then delerious there??[/color]

        Somewhat, yes. :)

        Development of DOM Level 3 began a while ago (the earliest date I could
        find was in 2000), but it has not been finished. Whilst the Core module
        (and two others) is a Proposed Recommendation, the Events module isn't
        even that far along so I would say that using it, especially relying on
        it, is a bad idea.

        initKeyEvent() is a method introduced in the DOM Level 3 Events Working
        Draft, written in 2000. The latest version of the Events module written
        last year has no such method: it has been renamed to initKeyboardEve nt().
        When the Events module finally becomes a Recommendation, it will probably
        use the latter. That method call you cited might be unsupported in future:
        the implementation it relies upon does, after all, used an unfinished
        document as its specification.

        KeyEvent, and therefore initKeyEvent(), has been totally removed from the
        latest edition. [guess] I think the change occurred due to the presence of
        a KeyEvent class existing in Java. [/guess].
        [color=blue][color=green]
        >> In a later version of the Document Object Model (DOM) Event
        >> specification[1], key events will be introduced.[/color]
        >
        > I read that too, but whats the point of mentioning that in the
        > documentation in first place... ;D[/color]

        It's an obvious omission in the specification. Other event types have been
        catered for, but not those fired from keyboard input.

        [snip]
        [color=blue][color=green]
        >> There is no actual need to simulate a tab press.[/color]
        >
        > I think it is.[/color]

        There's no *need*, but it would be easier.
        [color=blue]
        > How else would I know which is the previous or next form control ?[/color]

        By examining the DOM tree, determining the tab order, and giving focus
        appropriately.
        [color=blue]
        > A form control doesnt have properties like
        > previousFormCon trol nextFormControl[/color]

        No they don't, but Node interfaces have nextSibling and previousSibling
        properties. They won't work directly as you'd like, but moving through
        sibling after sibling, checking the element type as you go, will produce
        the same effect.
        [color=blue]
        > The whole point of why I wanted to 'stuff the keyboard buffer' with tab
        > char is that I wanted to let the browser do the underlying work of
        > moving the input focus and not me.[/color]

        I understand that but I don't think that is a reliable option at the
        moment. Walking the tree isn't either, necessarily, but it should be
        better supported than an unfinished standard.
        [color=blue]
        > If I have no fixed ID attributes and controller object that descibes the
        > relation and order of references of input elements how would I know
        > which is previous and which is next?[/color]

        The same way the browser does. Look up the tabindex attribute (URL below)
        in the HTML 4.01 Specification. It describes the tab order and how it is
        determined.
        [color=blue]
        > I dont think that as a ugly hack walking the tree and making descisions
        > upon it guarantees me the "natural order of form elements navigation".[/color]

        Walking the tree isn't a hack. If done properly, it will be a very
        successful method, but it will involve significant effort.
        [color=blue]
        > So what are the options?[/color]

        As I see them:

        - Use initKeyEvent() and initKeyboardEve nt() to simulate a tab press,
        using feature detection to determine support.
        - Walk the tree and, using the tab order defined by the HTML
        Specification, set focus yourself.

        The first requires the least effort, but is the most unreliable. The
        second option is the reverse. Which one you use is your choice.

        Mike


        tabindex attribute:

        http://www.w3.org/TR/html4/interact/...html#h-17.11.1

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Martin Honnen

          #5
          Re: programmaticall y generated keyboard events initKeyEvent



          Michael Winter wrote:

          [color=blue]
          > Development of DOM Level 3 began a while ago (the earliest date I could
          > find was in 2000), but it has not been finished. Whilst the Core module
          > (and two others) is a Proposed Recommendation, the Events module isn't
          > even that far along[/color]


          Even worse, the W3C has finished working on W3C DOM Level 3 Events, they
          have published a working group note
          http://www.w3.org/TR/2003/NOTE-DOM-L...ents-20031107/
          on Nov 7 2003 which means according to
          http://www.w3.org/2003/06/Process-20...tr.html#WGNote
          that the work on that topic has ended without reaching recommendation
          status.


          --

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

          Comment

          • Marek Mand

            #6
            Re: programmaticall y generated keyboard events initKeyEvent

            Michael Winter wrote:[color=blue]
            > Marek Mand <cador.soft@mai l.ee> wrote:[color=green]
            >> How else would I know which is the previous or next form control ?[/color]
            > By examining the DOM tree, determining the tab order, and giving focus
            > appropriately. Node interfaces have nextSibling and previousSibling
            > properties. They won't work directly as you'd like, but moving through
            > sibling after sibling, checking the element type as you go, will produce
            > the same effect.[/color]
            [][color=blue]
            > I don't think that is a walking the tree isn't either,
            > necessarily, but it should be better supported than an unfinished
            > standard.[/color]
            [][color=blue]
            > Walking the tree isn't a hack. If done properly, it will be a very
            > successful method, but it will involve significant effort.[/color]


            Hmm, so You propose the solution I was thinking of ;D
            By having some elements not having the tabIndex specified ans some
            having them specified it would be quite huge effort to walk all nodes
            and get the order right. Apart from that there are browser preferences
            "KEYBOARD NAVIGATION" in Mozilla IIRC which specify on what elements are
            taken into TABORDER by browser at all. The DOM tree certainly doesnt
            provide that info and there is no way to get the user preference under
            normal circumstances. ;D

            OK, but does anybody have such function that would implement the desired
            functionality so I dont have to reinvent a wheel?


            p.s. Thanks for the discussion, provided knowledge and opinions. =D

            --
            marekmand





            Comment

            • Michael Winter

              #7
              Re: programmaticall y generated keyboard events initKeyEvent

              On Wed, 03 Mar 2004 18:39:08 +0100, Martin Honnen <mahotrash@yaho o.de>
              wrote:
              [color=blue]
              > Even worse, the W3C has finished working on W3C DOM Level 3 Events, they
              > have published a working group note
              > http://www.w3.org/TR/2003/NOTE-DOM-L...ents-20031107/
              > on Nov 7 2003 which means according to
              > http://www.w3.org/2003/06/Process-20...tr.html#WGNote
              > that the work on that topic has ended without reaching recommendation
              > status.[/color]

              However, work on a report that has been given a Working Group Note status
              may resume at any time. It depends why that status was imposed. After
              looking through the last four years of the www-dom mailing list archives,
              I couldn't find the point when the module became a Working Group Note.
              However, it appears that the change was due to the number of issues
              raised: errors, incompatibiliti es, clarifications, etc.

              During my search, I did find that someone decided to spam the mailing list:



              What nerve!

              Mike

              --
              Michael Winter
              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

              Comment

              • Michael Winter

                #8
                Re: programmaticall y generated keyboard events initKeyEvent

                On Wed, 03 Mar 2004 19:50:44 +0200, Marek Mand <cador.soft@mai l.ee> wrote:
                [color=blue]
                > Michael Winter wrote:[color=green]
                >> Marek Mand <cador.soft@mai l.ee> wrote:[color=darkred]
                >>> How else would I know which is the previous or next form control ?[/color]
                >> By examining the DOM tree, determining the tab order, and giving focus
                >> appropriately. Node interfaces have nextSibling and previousSibling
                >> properties. They won't work directly as you'd like, but moving through
                >> sibling after sibling, checking the element type as you go, will
                >> produce the same effect.[/color]
                > [][color=green]
                > > I don't think that is a walking the tree isn't either,
                > > necessarily, but it should be better supported than an unfinished
                > > standard.[/color][/color]

                You mis-quoted me there. My grammar isn't that bad. :P
                [color=blue]
                > [][color=green]
                > > Walking the tree isn't a hack. If done properly, it will be a very
                > > successful method, but it will involve significant effort.[/color]
                >
                > Hmm, so You propose the solution I was thinking of ;D[/color]

                I only propose it as some browsers seem to implement the methods that
                would allow it. However, as those methods aren't standardised, I don't
                recommend the approach. I discounted it initially because I didn't think
                that browser developers would use an unfinished and, evidentally,
                problematic standard.
                [color=blue]
                > By having some elements not having the tabIndex specified ans some
                > having them specified it would be quite huge effort to walk all nodes
                > and get the order right.[/color]

                The only issue is that the specification doesn't say what value should
                assumed when the tabindex isn't specified - it's left to the browser.
                However, that would also mean it's up to you. I would probably use zero:
                process the elements with a tabindex first, then the remaining elements in
                order of appearance.
                [color=blue]
                > Apart from that there are browser preferences "KEYBOARD NAVIGATION"
                > in Mozilla IIRC which specify on what elements are taken into
                > TABORDER by browser at all.[/color]

                In your example, it wouldn't matter. INPUT elements of type text are
                *always* part of the tabbing order. Links can be excluded, as can buttons,
                radio buttons, checkboxes and SELECT elements. Links are one group; the
                other four are a second.
                [color=blue]
                > OK, but does anybody have such function that would implement the desired
                > functionality so I dont have to reinvent a wheel?[/color]

                Not me, sorry.
                [color=blue]
                > p.s. Thanks for the discussion, provided knowledge and opinions. =D[/color]

                You're very welcome.

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • Nathan Sweet

                  #9
                  Re: programmaticall y generated keyboard events initKeyEvent

                  > How else would I know which is the previous or next form control ?

                  It is your form.
                  [color=blue]
                  > The whole point of why I wanted to 'stuff the keyboard buffer' with tab
                  > char is that I wanted to let the browser do the underlying work of
                  > moving the input focus and not me.[/color]

                  This is not possible, unless you only want it to work in Mozilla
                  (which is what the link you posted is about).
                  [color=blue]
                  > If I have no fixed ID attributes and controller object that descibes the
                  > relation and order of references of input elements how would I know
                  > which is previous and which is next?
                  >
                  > I dont think that as a ugly hack walking the tree and making descisions
                  > upon it guarantees me the "natural order of form elements navigation".
                  >
                  > So what are the options?[/color]

                  Make it Mozilla specific or you can use the "elements" collection on
                  the form element, which is "a collection, in source order, of all
                  controls in a given form". The elements collection is in the W3C DOM1
                  spec so I assume it works in all modern browsers.

                  http://msdn.microsoft.com/library/de...s/elements.asp

                  An example:

                  function keyPressed () {
                  // If moo is typed...
                  if ( window.event.sr cElement.value != "moo" ) return;
                  // Figure the elements index.
                  var elements = document.getEle mentById( "someForm" ).elements;
                  for ( var elementIndex=0; elementIndex < elements.length ;
                  elementIndex++ )
                  if ( elements[elementIndex] == window.event.sr cElement )
                  break;
                  // Element not found.
                  if ( elementIndex == elements.length ) return;
                  // Otherwise, elementIndex is the index of the
                  window.event.sr cElement.
                  // Focus the next element.
                  elementIndex++;
                  if ( elementIndex == elements.length ) elementIndex = 0;
                  elements[ elementIndex ].focus();
                  }

                  But normally tab and shit+tab look at the tabindex property of
                  elements on the page and tabs to the next one based on that order, not
                  just source order.

                  It seems to me that people wouldn't code something like this to be
                  applied generically to any form. It seems like it has a specific
                  application, and in that case you control the form and so can get the
                  exact functionality necessary.

                  Comment

                  • Marek Mand

                    #10
                    Re: programmaticall y generated keyboard events initKeyEvent

                    Michael Winter wrote:[color=blue]
                    > On Wed, 03 Mar 2004 19:50:44 +0200, Marek Mand <cador.soft@mai l.ee> wrote:[color=green]
                    >> By having some elements not having the tabIndex specified ans some
                    >> having them specified it would be quite huge effort to walk all nodes
                    >> and get the order right.[/color]
                    > The only issue is that the specification doesn't say what value should
                    > assumed when the tabindex isn't specified - it's left to the browser.
                    > However, that would also mean it's up to you. I would probably use zero:
                    > process the elements with a tabindex first, then the remaining elements
                    > in order of appearance.[/color]

                    Yes, and another of the shortcomings with this HTML doc referenced by
                    you is that it doesnt alloes only integers from 0 up to 32xxx and doesnt
                    seem to allow values such as -1 for tabIndex, which de facto specify
                    that the element doesnt take part of tabbing que.
                    [color=blue][color=green]
                    >> Apart from that there are browser preferences "KEYBOARD NAVIGATION"
                    >> in Mozilla IIRC which specify on what elements are taken into
                    >> TABORDER by browser at all.[/color]
                    > In your example, it wouldn't matter. INPUT elements of type text are
                    > *always* part of the tabbing order. Links can be excluded, as can
                    > buttons, radio buttons, checkboxes and SELECT elements. Links are one
                    > group; the other four are a second.[/color]

                    True, but later on I was thinking of more generic function, which would
                    be applicable for links and inputs and textareas and selects.
                    Must do the hard labour all by myself ;D

                    --
                    marekmand


                    Comment

                    Working...