Search code examples
javagwtgwtquery

Mimicking jQuery event.which in GWT


The GwtQuery documentation provides the following example as a starting point for fiddling with events:

$("h1").bind(Event.ONMOUSEOVER | Event.ONMOUSEOUT, new Function() {
  public boolean f(Event e) {
    $(e).toggleClass("highlight");
    return true;
  }
});

However, unlike the jQuery parallel, there is no this keyword to refer to the element inside the handler, that's why it is passed as Event e, to be wrapped within $(). But then, we don't have access to the actual event. How do we calculate in GWT what we can in jQuery with event.which, or event.target?

Specifically, I am looking for two events. One is a mousedown, after which I need to check whether it was the left button (jQuery equivalent being e.which == 1), and a keyup event, after which I need to check for specific keys (e.keyCode == 13, etc).


Solution

  • The Event object passed to the function is the GWT object com.google.gwt.user.client.Event So if you want to know if the left button was pressed :

    if (e.getButton() == NativeEvent.BUTTON_LEFT){
     ...
    }
    

    if you want to know which key was pressed: e.getCharCode()