tags:

views:

219

answers:

2

In a GWT app, I would like to track and display X, Y mouse coordinates and intercept clicks over the whole browser page. The page contains lots of GWT widgets such as panels, buttons, etc... Any advise would be appreciated.

Thanks. Daniel

+2  A: 

Take a look at: http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/Event.html#addNativePreviewHandler%28com.google.gwt.user.client.Event.NativePreviewHandler)

Make sure that you install a NativePreviewHandler and also make sure that you have a MouseMouseHandler registered on the RootPanel or another widget that covers the browser window.

David Nouls
+1  A: 

I have had to use these a couple of times recently for various reason. This is a very basic example of how to use the GWT native preview handler stuff.

I have one bit of warning to note though: onPreviewNativeEvent() will be executed.... often. If you put any sort of computationally expensive logic in here, it will slow everything down, especially in IE and/or on older computers. Depending on your needs, it might be a non issue, but it is worth mentioning.

Event.addNativePreviewHandler(new NativePreviewHandler() {
  public void onPreviewNativeEvent(final NativePreviewEvent event) {
    final int eventType = event.getTypeInt();
    switch (eventType) {
      case Event.ONMOUSEMOVE:
        //mouse tracking logic?
        break;
      case Event.ONCLICK:
        final int eventX = event.getNativeEvent().getClientX();
        final int eventY = event.getNativeEvent().getClientY();
        Window.alert("Clicked @ " + eventX + "," + eventY);
        break;
      default:
        // not interested in other events
    }
  }
});
bikesandcode