为什么GWT不能在文档元素上添加关键事件处理程序? [英] Why doesn't GWT let us add key event handlers on document element?

查看:141
本文介绍了为什么GWT不能在文档元素上添加关键事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以附加这些处理程序的 FocusPanel ,但根据我的经验,这个组件的表现并不那么好。所以我想尽可能地避免它。

I know there's FocusPanel on which I can attach such handlers, but in my experience this component does not behave that well. So I'd like to avoid it as much as possible.

所以我想知道为什么没有办法在文档上附加关键处理程序?根据 quirksmode.org ,它可以跨浏览器工作,所以这不应该是一个关注。

So I'm wondering why there's no way to attach key handlers on document too? According to quirksmode.org it works cross-browser, so this shouldn't be a concern.

我也尝试编写一些JSNI代码来自己做,这在大多数情况下都可以正常工作。但是,如果有任何其他小部件侦听与文档相同的事件,并且该小部件允许事件传播,对于到达该文档的事件,我几乎可以做任何事情,因为它被标记为死亡,并且异常将被当我尝试访问该事件的数据时抛出。

I've also tried writing some JSNI code to do this myself, which works ok for most cases. However, if there's any other widget that listens for the same event as me on document, and that widget lets the event propagate, I can do pretty much nothing with the event that reached the document, because it's marked as dead and an exception will be thrown whenever I try to access data on that event.

这是我的代码到目前为止:

Here's my code so far:

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);
    }
  }-*/;
}


推荐答案

/ p>



How about the following?

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

它将它们添加到文档的正文上,但没有太大的不同。

It adds them on the document's body, but that's not much different.

这篇关于为什么GWT不能在文档元素上添加关键事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆