java - 当光标不在文本输入或区域上时触发浏览器返回

标签 java gwt

当鼠标光标不在文本输入或文本区域上时,如何使用 GWT 使浏览器返回触发?

但是,当它位于文本输入或文本区域时,按后退应该起到文本退格的作用,并且不会触发浏览器后退?

最佳答案

像这样的东西应该可以工作。只需调用注册方法即可设置监听器。

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();
    }
}

关于java - 当光标不在文本输入或区域上时触发浏览器返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530607/

相关文章:

java - 在以邻接表形式表示的二叉树中循环

java - 在 Java 中用于程序化 PDF 生成的 iText 是否有可靠的替代品?

gwt - 我可以在使用 GWT UIRenderer 时以某种方式重复 HTML 片段吗?

gwt - GWT 的进度条

gwt - TabLelayoutPanel 将选项卡向右对齐

java - InputStream 中的 skip() 方法是如何工作的?

java - 我是否必须手动列出 Java POM 中 <resources> 内的所有子目录

java - 使用字典和散列破解密码

java - GWT - DialogBox 玻璃和 GoogleChrome - 玻璃不重绘

javascript - gwt 和 javascript 文件