tags:

views:

71

answers:

1

I'm trying to change the row highlighting in my FlexTable using KeyCodes.KEY_UP/DOWN. This doesn't seem to work (based on 1809155):

public class KeyAwareFlexTable extends FlexTable 
      implements KeyDownHandler, HasKeyDownHandlers {
   public KeyAwareFlexTable() {
      this.addKeyDownHandler(this);
   }

   @Override
   public void onKeyDown(KeyDownEvent event) {
      GWT.log("onKeyDown");
      // check if up/down & do something useful
   }

   @Override
   public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
      return addDomHandler(handler, KeyDownEvent.getType());
   }
}

I've also tried this (based on this site):

FlexTable table = new FlexTable() {
  @Override
  public void onBrowserEvent(Event event) {
     super.onBrowserEvent(event);
     GWT.log("Event type = " + DOM.eventGetType(event));
     switch (DOM.eventGetType(event)) {
     case Event.ONKEYDOWN:
        if (DOM.eventGetKeyCode(event) == KeyCodes.KEY_UP) {
           GWT.log("up");
        } else if (DOM.eventGetKeyCode(event) == KeyCodes.KEY_DOWN) {
           GWT.log("down");
        }
        break;
     default:
        break;
     }
   }
};
table.sinkEvents(Event.ONKEYDOWN);

I'm looking for a way to have this behavior more or less. Does anybody have a way to do this in GWT?

A: 

Got it

  FocusPanel focus = new FocusPanel(flexTable);
  focus.addKeyDownHandler(new KeyDownHandler() {
     @Override
     public void onKeyDown(KeyDownEvent event) {
        int code = event.getNativeKeyCode();
        if (KeyCodes.KEY_UP == code) {
           GWT.log("Bed goes up");
        } else if (KeyCodes.KEY_DOWN == code) {
           GWT.log("Bed goes down");
        }
     }
  });
Eric Landry
Also, adding event.preventDefault() helps me swallow this event so that the browser doesn't try to scroll.
Eric Landry