当鼠标光标不在文本输入或文本区域时,如何使用GWT触发浏览器?
然而,当它在文本输入或文本区域时,按下后应该作为文本退格而不是触发浏览器回来?
答案 0 :(得分:1)
这样的事情应该有效。只需调用register方法即可设置监听器。
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.History;
public class BackPreviewHandler implements NativePreviewHandler {
private HandlerRegistration handlerRegistration;
@Override
public void onPreviewNativeEvent(final NativePreviewEvent event) {
if (event.getTypeInt() == Event.ONKEYDOWN) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE) {
Element elemPointedByMouse = Element.as(event.getNativeEvent().getEventTarget());
String tagName = elemPointedByMouse.getTagName();
System.out.println(tagName);
if (!tagName.equalsIgnoreCase("INPUT")
&& !tagName.equalsIgnoreCase("TEXTAREA")) {
History.back();
}
}
}
}
public void register() {
handlerRegistration = Event.addNativePreviewHandler(this);
}
public void deregister() {
handlerRegistration.removeHandler();
}
}