简体   繁体   中英

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. Say, I'm using it on widget 'B', and hitting the backpsace when widget 'B' is focused should take me to widget '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! I want my backspace event to be used only by my (GWT) code, not the browser.

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.

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); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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