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

查看:9
本文介绍了为什么 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.

这是我目前的代码:

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

推荐答案

以下内容如何?

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

它将它们添加到文档正文中,但没有太大区别.

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

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

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