I know there is a FocusPanel on which I can attach such handlers, but in my experience this component does not behave very well. Therefore, I would like to avoid this as much as possible.
So, I wonder why there is no way to attach key handlers on a document? According to quirksmode.org , it runs a cross browser, so this should not be a concern.
I also tried writing JSNI code to do it myself, which works fine for most cases. However, if you have another widget that listens for the same event as I am in the document, and this widget allows you to distribute the event, I can do almost nothing with the event that reached the document, because it is marked as dead, and the exception will be when I try to access the data of this event.
Here is my code:
public class RichDocument implements HasKeyPressHandlers, HasKeyDownHandlers, HasKeyUpHandlers, HasClickHandlers { private static final RichDocument instance = new RichDocument(); public static RichDocument get() { return instance; } private final EventBus eventBus = new SimpleEventBus(); private RichDocument() { startListening(); } @Override public HandlerRegistration addClickHandler(ClickHandler handler) { return eventBus.addHandler(ClickEvent.getType(), handler); } @Override public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) { return eventBus.addHandler(KeyDownEvent.getType(), handler); } @Override public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) { return eventBus.addHandler(KeyPressEvent.getType(), handler); } @Override public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) { return eventBus.addHandler(KeyUpEvent.getType(), handler); } @Override public void fireEvent(GwtEvent<?> event) { eventBus.fireEvent(event); } private native void startListening(); }
source share