Over which window is the mouse?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Mlynarczyk

    Over which window is the mouse?

    Hi,

    I have a JavaScript application which opens a second window. Which is the
    best way to detect over which (if any) of the two windows the mouse is?
    First I thought document.onmous eout and document.onmous eover (for both
    windows) would be the way to do it, but some browsers (like Firefox) seem to
    fire these even when the mouse passes within the document (like from the
    "empty body background" to a div), and anyway, any child elements of
    document would "get" the events before the document (event bubbling), so my
    script would still "think" the mouse is over the other window. Using
    onfocus/onblur does not seem to be the right idea either as it would require
    something like a click in a window to trigger them. Now the only more or
    less promising way I can think of is to set a reference to the respective
    window on each object I want to handle events for. But then I'd have to take
    care about the cyclic references this would create. So what could I do? Is
    there a simple solution?

    Thanks in advance for your advice,
    Thomas



  • Jay

    #2
    Re: Over which window is the mouse?


    "Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message
    news:d65ncn$q8i $03$1@news.t-online.com...[color=blue]
    > Hi,
    >
    > I have a JavaScript application which opens a second window. Which is the
    > best way to detect over which (if any) of the two windows the mouse is?[/color]

    I might be missing something but wouldn't you want to check which window had
    focus?

    Jay

    [color=blue]
    > First I thought document.onmous eout and document.onmous eover (for both
    > windows) would be the way to do it, but some browsers (like Firefox) seem
    > to
    > fire these even when the mouse passes within the document (like from the
    > "empty body background" to a div), and anyway, any child elements of
    > document would "get" the events before the document (event bubbling), so
    > my
    > script would still "think" the mouse is over the other window. Using
    > onfocus/onblur does not seem to be the right idea either as it would
    > require
    > something like a click in a window to trigger them. Now the only more or
    > less promising way I can think of is to set a reference to the respective
    > window on each object I want to handle events for. But then I'd have to
    > take
    > care about the cyclic references this would create. So what could I do? Is
    > there a simple solution?
    >
    > Thanks in advance for your advice,
    > Thomas
    >
    >
    >[/color]


    Comment

    • shlomi.schwartz@gmail.com

      #3
      Re: Over which window is the mouse?

      Hi Thomas,

      I think that I have a solution for you.

      1) Attach an onMouseMove event to the body of both windows.
      2) The handler functions should change a variable which will indicate
      the "Hovered" window name or id

      than you can check this variable to get your answer.
      from the second window use window.opener.h andlerName

      I Tried this solution on firefox ... and it's OK.

      good luck

      Comment

      • Thomas Mlynarczyk

        #4
        Re: Over which window is the mouse?

        Also sprach Jay:
        [color=blue]
        > "Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message
        > news:d65ncn$q8i $03$1@news.t-online.com...[color=green]
        >> Hi,
        >>
        >> I have a JavaScript application which opens a second window. Which
        >> is the best way to detect over which (if any) of the two windows the
        >> mouse is?[/color]
        >
        > I might be missing something but wouldn't you want to check which
        > window had focus?[/color]

        If window A has the focus and I simply move the mouse (no button pressed, no
        clicking, nor anything) to window B - would that alone change the focus
        already?



        Comment

        • Thomas Mlynarczyk

          #5
          Re: Over which window is the mouse?

          Also sprach shlomi.schwartz @gmail.com:
          [color=blue]
          > 1) Attach an onMouseMove event to the body of both windows.
          > 2) The handler functions should change a variable which will indicate
          > the "Hovered" window name or id[/color]

          Thank you for this solution. There is just one point that bothers me: Say I
          have a div in the second window which is right at the edge of the canvas. If
          I move the mouse over that window so that it enters where the div is,
          wouldn't the div get the onmouseover event first - before the event has
          bubbled up to the document and thus before the "whichWindo w" variable has
          been changed?



          Comment

          • shlomi.schwartz@gmail.com

            #6
            Re: Over which window is the mouse?

            Thomas,
            It is true that the DIV will catch the event before the body and than
            the event will bubble up to the body.

            what exactly are you trying to achieve?

            Comment

            • Thomas Mlynarczyk

              #7
              Re: Over which window is the mouse?

              Also sprach shlomi.schwartz @gmail.com:
              [color=blue]
              > Thomas,
              > It is true that the DIV will catch the event before the body and than
              > the event will bubble up to the body.
              >
              > what exactly are you trying to achieve?[/color]

              Well, among other things I want to update an object with mouse position
              information and in order to calculate that, it is necessary for some
              browsers to use document.scroll Left and similar properties and that's why I
              need to know which document object to use.

              Meanwhile I have come up with another idea:

              object.onSomeEv ent = function (e) { return window.opener.m yEventHandler(e ||
              null, window, this); };

              If I'm not wrong the anonymous function should always be evaluated in the
              context of the window in which it was called and therefore the window
              parameter should point to the object's window. For objects in the main
              window, I would assign myEventHandler directly. I just wonder if this is
              really the best way to do this or if there are drawbacks (performance
              penalty due to nested function calls).


              Comment

              • shlomi.schwartz@gmail.com

                #8
                Re: Over which window is the mouse?

                A creative idea!
                I guess [event.x , event.y , event.screenX , event.screenY] are not
                cross browser compatible ?

                anyway, I hope I helped.

                Comment

                • Thomas Mlynarczyk

                  #9
                  Re: Over which window is the mouse?

                  Also sprach shlomi.schwartz @gmail.com:
                  [color=blue]
                  > A creative idea![/color]

                  Not creative enough, it seems. My test showed that the anonymous function
                  will use the "window" in which it was defined rather than the one from which
                  it was called. I'd have to define it in the new window, which is a bit
                  awkward as I want to keep all the code in my main window and use the same
                  syntax for every element, no matter in which window it is. So, in theory,
                  the simplest thing would be to "backtrace" the window of an element using
                  the parentNode property in a loop. But that would mean a huge performance
                  penalty, I guess. So, I still haven't found a solution yet.

                  Ah, yes, with the onfocus - the problem is, to get the event object, IE uses
                  window.event, so I must know *which* window before I can get it. Also, I
                  have some functions doing display-related things (like adding a node to the
                  document tree) and these need to know the window as well (for stuff like
                  document.getEle mentById()). But as these could be called independently of
                  any event, an event-based solution would not work.
                  [color=blue]
                  > I guess [event.x , event.y , event.screenX , event.screenY] are not
                  > cross browser compatible ?[/color]

                  event.screenX and event.screenY yes, the others not. Every browser has its
                  own way of providing mouse position information, so I actually use a rather
                  complex thing like "if A is supported, use it, else if B is supported, use
                  that etc.". This way, I can get the mouse position relative to the screen,
                  the viewport, the document and the element.



                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Over which window is the mouse?

                    Thomas Mlynarczyk wrote:
                    [color=blue]
                    > If window A has the focus and I simply move the mouse (no button pressed,
                    > no clicking, nor anything) to window B - would that alone change the focus
                    > already?[/color]

                    That depends on the window framework and it entirely possible.


                    PointedEars
                    --
                    Wieso? Das ist die hochentwickelte Sensorik des OS.
                    Es erkennt ja auch, wenn der Nutzer gerade wichtige Aufgaben zu
                    erledigen hat und zeigt dies dann prägnant mit einem großen blauen
                    grafischen Bedienelement an. ;-) -- Andreas Borutta in dciwam

                    Comment

                    • Randy Webb

                      #11
                      Re: Over which window is the mouse?

                      Thomas 'PointedEars' Lahn wrote:[color=blue]
                      > Thomas Mlynarczyk wrote:
                      >
                      >[color=green]
                      >>If window A has the focus and I simply move the mouse (no button pressed,
                      >>no clicking, nor anything) to window B - would that alone change the focus
                      >>already?[/color][/color]

                      No. The act, alone, of mousing over a window does not give it focus. Put
                      an onfocus event handler and test it. Giving a window an onmouseover
                      event handler to grab focus when its moused over would cause it to have
                      focus.
                      [color=blue]
                      >
                      > That depends on the window framework and it entirely possible.[/color]

                      Not simply by mousing over the window, there has to be more involved
                      than that.

                      --
                      Randy
                      comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                      Comment

                      • Random

                        #12
                        Re: Over which window is the mouse?

                        Mr. Lahn has a point: Some shells and window managers WILL
                        automatically move the focus as a window is moused-over. This is most
                        common on Linux-based operating systems, but you'll also see it
                        frequently in any *NIX GUI shell (including some configurations of Mac
                        OS X). I'm actually using a Windows shell (BlackBox for Windows) that
                        does allow me to do that.

                        Just something to keep in mind.

                        I'd go with Mr. Webb's solution of an onMouseOver handler that grabs
                        focus.

                        Comment

                        • Randy Webb

                          #13
                          Re: Over which window is the mouse?

                          Random wrote:[color=blue]
                          > Mr. Lahn has a point: Some shells and window managers WILL
                          > automatically move the focus as a window is moused-over. This is most
                          > common on Linux-based operating systems, but you'll also see it
                          > frequently in any *NIX GUI shell (including some configurations of Mac
                          > OS X). I'm actually using a Windows shell (BlackBox for Windows) that
                          > does allow me to do that.
                          >
                          > Just something to keep in mind.
                          >
                          > I'd go with Mr. Webb's solution of an onMouseOver handler that grabs
                          > focus.[/color]

                          Would you also go with Mr. Webb's standard practice of quoting what I am
                          replying to? After all, it *is* the recommended practice in this group.

                          --
                          Randy
                          comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                          Comment

                          • Random

                            #14
                            Re: Over which window is the mouse?

                            Now that Mr. G has informed him how to get Google Groups to do so, Mr.
                            R would consider it, yes, but not so much on the strength of Mr. Webb's
                            recommendation as on the courtesy of Mr. G's request.

                            -R

                            -------------------------------------------
                            Sarcastic little bit, isn't he, that Mr. ---.
                            -------------------------------------------
                            Mr. Pink: How about if I'm Mr. Purple? That sounds good to me, I'll be
                            Mr. Purple.
                            Joe: You're not Mr. Purple. Some guy on some other job is Mr. Purple.
                            You're Mr. Pink!
                            Mr. White: Who cares what your name is?
                            Mr. Pink: Yeah that's easy for you to say, you're Mr. White, you have a
                            cool sounding name. All right look if it's no big deal to be Mr. Pink,
                            do you wanna trade?

                            -- From the script of Reservoir Dogs, by Quentin Tarantino.


                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: Over which window is the mouse?

                              Random wrote:
                              [color=blue]
                              > Mr. Lahn has a point: Some shells and window managers WILL
                              > automatically move the focus as a window is moused-over. This
                              > is most common on Linux-based operating systems, but you'll
                              > also see it frequently in any *NIX GUI shell (including some
                              > configurations of Mac OS X).[/color]

                              Exactly.
                              [color=blue]
                              > I'm actually using a Windows shell (BlackBox for Windows) that
                              > does allow me to do that.[/color]

                              With Microsoft PowerToys' Control Panel feature,
                              no special Windows shell is required for that.


                              PointedEars

                              Comment

                              Working...