Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking mouse movements over a page with GWT

Tags:

gwt

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

like image 461
supercobra Avatar asked Nov 12 '09 23:11

supercobra


2 Answers

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
    }
  }
});
like image 128
bikesandcode Avatar answered Oct 01 '22 19:10

bikesandcode


Take a look at: http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/Event.html#addNativePreviewHandler(com.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.

like image 40
David Nouls Avatar answered Oct 01 '22 20:10

David Nouls