Raph Levien
Raph Levien

Reputation: 5218

How do I attach an event handler to the document or window in GWT?

In a GWT app centered around a canvas, I'm having trouble keeping focus directed in the right place - particularly for keyboard shortcuts. For now, I've wrapped the canvas in a FocusPanel, but that causes the canvas to not respond to the RequiresResize protocol, because FocusPanel does not plumb that.

A second (related, I think) problem is that the FocusPanel is not getting Ctrl-A keypress events at all (tested on Mac Chrome). I can get Ctrl-Z and other keys (such as arrows) just fine.

In a pure JavaScript world, I think the best answer to this would be to attach mouse and key handlers to the document or window object (I'm not positive which is better). However, I don't see an obvious way to do this in GWT - in particular, the Document and Window classes lack methods for attaching these kind of event handlers?

Anyone know how to do it, or, perhaps, to solve the more general problem of keeping focus on an appropriate widget able to handle keyboard shortcuts?

Upvotes: 1

Views: 1148

Answers (1)

Jason Terk
Jason Terk

Reputation: 6025

You can solve your first problem by extending FocusPanel to implement the RequiresResize and ProvidesResize interfaces:

public class ResizingFocusPanel
  extends FocusPanel
  implements RequiresResize, ProvidesResize
{
  public ResizingFocusPanel() {}

  public ResizingFocusPanel(Widget child) {
    super(child);
  }

  @Override
  public void onResize() {
    if (getWidget() instanceof RequiresResize) {
      ((RequiresResize)getWidget()).onResize();
    }
  }
}

Upvotes: 3

Related Questions