Key handler for Grid

Hello,

We switched from 7.3 to 7.4 in order to make use of new Grid on one of our screens. This screen resembles a typical mail-client interface and navigation with keys is crucial. We used to have grid layout with custom action handlers to implement this. Grid which was introduced in 7.4 has built-in support for navigation with keys.

How can I:

  1. Attach a listener for keyboard events (up/down etc.) ?
  2. Find out which cell is currently in focus?

I was thinking of using ShortcutListener to do item # 2, but it turned out to be not useful at all since what I get in handleAction() is an instance of UI and an instance of Grid.

Any help or suggestions are welcome.

Thanks,
Andrew

Ok, I seem to have managed to solve my problem by using custom component connector:

@Connect(com.vaadin.ui.Grid.class) public class ExtendedVaadinGridConnector extends GridConnector { @Override protected void init() { super.init(); getWidget().addBodyKeyUpHandler(new BodyKeyUpHandler() { @Override public void onKeyUp(GridKeyUpEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_DOWN || event.getNativeKeyCode() == KeyCodes.KEY_UP) { CellReference cell = event.getFocusedCell(); String rowKey = getRowKey((JsonObject) cell.getRow()); String columnId = getColumnId(cell.getColumn()); getRpcProxy(GridServerRpc.class).itemClick(rowKey, columnId, new MouseEventDetails()); } } }); } } You also need to add this to your gwt.xml file:

<replace-with class="com.ui.widgets.client.grids.ExtendedVaadinGridConnector"> <when-type-is class="com.vaadin.client.connectors.GridConnector"/> </replace-with> In essence, when user presses up or down key, you get an ItemClickEvent which you can handle via addItemClickListener().

C’mon guys, you’re designing public API for your clients… Why haven’t you added this method to server-side API in the first place?