Why doesn't GWT allow key event handlers to be added to a document item?

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()/*-{ var self = this; var fire = function (event) { event = event || $wnd.event; @com.google.gwt.event.dom.client.DomEvent::fireNativeEvent(Lcom/google/gwt/dom/client/NativeEvent;Lcom/google/gwt/event/shared/HasHandlers;)(event, self); }; if ($wnd.document.addEventListener) { $wnd.document.addEventListener("click", fire, false); $wnd.document.addEventListener("keydown", fire, false); $wnd.document.addEventListener("keypress", fire, false); $wnd.document.addEventListener("keyup", fire, false); } else { $wnd.document.attachEvent("onclick", fire); $wnd.document.attachEvent("onkeydown", fire); $wnd.document.attachEvent("onkeypress", fire); $wnd.document.attachEvent("onkeyup", fire); } }-*/; } 
+4
source share
1 answer

What about the next one?

 RootPanel.get().addDomHandler(handler, KeyDownEvent.getType()); 

He adds them to the body of the document, but that is not much different.

+9
source

All Articles