views:

209

answers:

1

In my GWT program I have a table that has a selected row. I'd like to move the row selection with the up- and down-keys on the keyboard. So I have to catch the key events somehow.

The GWT docs handle key events in input fields only. But I don't have an input field!

Is this possible at all? Maybe it is a DOM/Javascript restriction that GWT cannot work around...

A: 

It works by using Event.addNativePreviewHandler(NativePreviewHandler handler)

But there are some things to consider:

  • The handler is not restricted to a widget. It is global for your application. If you change widgets you might have to register and unregister the handler manually.
  • There are browser differences with keyboard events. Some browsers send keyDown- and keyPress-Events, others just keyDown-Events.

To work around the second issue you can get the name of the browser using this code:

private static native String getUserAgent() /*-{
  return navigator.userAgent.toLowerCase();
}-*/;
Olaf Mertens