简体   繁体   English

GWT:“后退按钮”按浏览器和我的GWT代码两次工作

[英]GWT: Back Button working twice, both by browser and my GWT code

The Scenario: 场景:

In my GWT webapp, I'm using KeyDownHandler to capture the event of user hitting backspace. 在我的GWT KeyDownHandler应用程序中,我使用KeyDownHandler捕获用户KeyDownHandler退格键的事件。 Say, I'm using it on widget 'B', and hitting the backpsace when widget 'B' is focused should take me to widget 'A'. 说,我在小部件“ B”上使用它,而当小部件“ B”聚焦时击中后背,应该把我带到小部件“ A”。

The Problem: 问题:

On hitting backspace, I'm taken to widget 'A', BUT only for a moment before the Browser takes me back to the previous page! 按下退格键后,我将被带到窗口小部件“ A”,但只有片刻,然后浏览器才将我带回到上一页! I want my backspace event to be used only by my (GWT) code, not the browser. 我希望退格事件仅由我的(GWT)代码使用,而不由浏览器使用。

final TextBox txtA = new TextBox();
    TextBox txtB = new TextBox();

    VerticalPanel testPanel = new VerticalPanel();
    testPanel.add(txtA);
    testPanel.add(txtB);

    txtB.addKeyDownHandler(new KeyDownHandler() {

        public void onKeyDown(KeyDownEvent event) {
            if(event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE){
                Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                    public void execute() {
                        txtA.setFocus(true);
                    }
                });
            }
        }
    });

    RootPanel.get().add(testPanel);

This SO post might be usefull to you. 这样的帖子可能对您有用。 However its still better to use shift+tab to navigate backwards in a web-based form IMHO. 但是,最好还是使用shift + tab以基于Web的形式IMHO向后导航。

You might have to prevent the default action from happening. 您可能必须防止发生默认操作 And you might have to do it for some or all of the key events (keydown, keypress –in Firefox–, keyup); 您可能需要对某些或所有按键事件(例如Firefox中的keydown,keypress和keyup)执行此操作; be sure to test as many browsers as possible! 请确保测试尽可能多的浏览器!

That being said, hijacking global keyboard shortcuts is seen my many users as being too intrusive; 话虽这么说,劫持全局键盘快捷键被我许多用户视为过于侵入。 and it might have consequences on accessibility of your app. 可能会影响您应用的可访问性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM