mousemove listener for document?

720 views
Skip to first unread message

markww

unread,
Jan 25, 2010, 7:49:43 PM1/25/10
to Google Web Toolkit
Hi,

Is there a way to listen for mousemoves on the main document? In
javascript, I usually do this:

window.onload = function() {
document.onmousemove = function(e) {
alert("the mouse was moved!");
};
}

can we add some sort of similar listener in GWT? I'm just not sure
where to start,

Thanks

DaveC

unread,
Jan 26, 2010, 4:39:24 AM1/26/10
to Google Web Toolkit
The way I've done it (I think is):

1. Create a new Class that extends Widget and implements
HasMouseMoveHandlers

2. Grab the RootPanel Element and pass that into your new Class
constructor (the one that takes an Element as a param) - this should
call setElement(Element element) with you passed in element...

You can now add MouseMove handlers to you new Widget...

Fazeel Kazi

unread,
Jan 26, 2010, 6:08:18 AM1/26/10
to google-we...@googlegroups.com
Another simpler solution using EventPreview:

Event.addNativePreviewHandler(new Event.NativePreviewHandler()
{
     public void onPreviewNativeEvent(NativePreviewEvent foEvent)
     {  
        switch(foEvent.getTypeInt())
        {
           case Event.ONCLICK: handleClick(foEvent); break;
           case Event.ONMOUSEOVER: handleMouseOver(foEvent); break;
        }// end switch
     }
});


Regards.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


DaveC

unread,
Jan 26, 2010, 6:17:27 AM1/26/10
to Google Web Toolkit
I've run into (perfornance) issues using a NativePreviewHandler as it
will recieve *ALL* events (mouseover/out/move/up/down etc, etc...)
which is why I don't use that method.

On Jan 26, 11:08 am, Fazeel Kazi <fazzze...@gmail.com> wrote:
> Another simpler solution using EventPreview:
>
> Event.addNativePreviewHandler(new Event.NativePreviewHandler()
> {
>      public void onPreviewNativeEvent(NativePreviewEvent foEvent)
>      {
>         switch(foEvent.getTypeInt())
>         {
>            case Event.ONCLICK: handleClick(foEvent); break;
>            case Event.ONMOUSEOVER: handleMouseOver(foEvent); break;
>         }// end switch
>      }
>
> });
>
> Regards.
>
> On Tue, Jan 26, 2010 at 3:09 PM, DaveC

> <david.andrew.chap...@googlemail.com>wrote:


>
> > The way I've done it (I think is):
>
> > 1. Create a new Class that extends Widget and implements
> > HasMouseMoveHandlers
>
> > 2. Grab the RootPanel Element and pass that into your new Class
> > constructor (the one that takes an Element as a param) - this should
> > call setElement(Element element) with you passed in element...
>
> > You can now add MouseMove handlers to you new Widget...
>
> > On Jan 26, 12:49 am, markww <mar...@gmail.com> wrote:
> > > Hi,
>
> > > Is there a way to listen for mousemoves on the main document? In
> > > javascript, I usually do this:
>
> > >   window.onload = function() {
> > >       document.onmousemove = function(e) {
> > >           alert("the mouse was moved!");
> > >       };
> > >   }
>
> > > can we add some sort of similar listener in GWT? I'm just not sure
> > > where to start,
>
> > > Thanks
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>

Fazeel Kazi

unread,
Jan 26, 2010, 10:28:15 PM1/26/10
to google-we...@googlegroups.com
Are u sure abt that? I haven't done any testing in gwt for it. If u hv done, please share the details since have started using this extensively in my project.

My experience from pure javascript is that one single handler at document level is much better that having several handlers on various buttons and anchors.

Thanks.


To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

DaveC

unread,
Jan 29, 2010, 5:10:48 AM1/29/10
to Google Web Toolkit
"My experience from pure javascript is that one single handler at
document
level is much better that having several handlers on various buttons
and
anchors."

Yep - agreed - but we aren't dealing with "pure javascript" this is a
toolkit...

From the GWT class:

"Adds a Event.NativePreviewHandler that will receive all events before
they are fired to their handlers. Note that the handler will receive
all native events, including those received due to bubbling, whereas
normal event handlers only receive explicitly sunk events. "

The important bits from that paragraph are "all events before they are
fired to their handlers" and "will receive all native events,
including those received due to bubbling".

The effect I've seen is that whilst this handler is attached and
"doing something", e.g. handling a mousemove, any other *event*
handlers don't get fired e.g. animations stop working, until the mouse
has stopped moving...

What they've tried to do with GWT (from my point of view at least) is
make it like Java Swing - which, if I add a ClickListener to a Swing
Button that event doesn't get bubbled, I'd expicitly have to
"manually" fire the event on it's ancestors...(I think - I'm no swing
expert!) working with the DOM (and all its cross browser quirks) is
very different to writing a Swing App (or similar)...

+ I *think* GWT uses event delegation anyway.

Cheers,
Dave


On Jan 27, 3:28 am, Fazeel Kazi <fazzze...@gmail.com> wrote:
> Are u sure abt that? I haven't done any testing in gwt for it. If u hv done,
> please share the details since have started using this extensively in my
> project.
>
> My experience from pure javascript is that one single handler at document
> level is much better that having several handlers on various buttons and
> anchors.
>
> Thanks.
>
> On Tue, Jan 26, 2010 at 4:47 PM, DaveC

> <david.andrew.chap...@googlemail.com>wrote:

> > <google-web-toolkit%2Bunsu...@googlegroups.com<google-web-toolkit%252Buns...@googlegroups.com>

Craig Mitchell

unread,
Sep 20, 2011, 12:48:47 AM9/20/11
to google-we...@googlegroups.com
With the later versions of GWT, we can just do this:

// Listen for a mouse move anywhere on the page
MouseMoveHandler moveHandler = new MouseMoveHandler() {
@Override
public void onMouseMove(MouseMoveEvent event) {
System.out.println("Mouse move " + event.getClientX() + " : " + event.getClientY());
}
};
RootPanel.get().addDomHandler(moveHandler, MouseMoveEvent.getType());
Reply all
Reply to author
Forward
0 new messages