Trigger browser back when cursor is not on a text input or area

quarks

How do make the browser back trigger with GWT, when the mouse cursor is not on a text input or text area?

However when its on a text input or text area, pressing back should function as a text backspace and not trigger browser back?

Alkis Kalogeris

Something like this should work. Just call the register method in order to set a the listener.

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Trigger an event on back button in browser/android with javascript

분류에서Dev

ActionListener to trigger on text input from JTextField

분류에서Dev

ios7 webapp with appcache, when trigger history.back

분류에서Dev

Display input from text area as HTML and assign IDs to the HTML elements

분류에서Dev

Disable long press menu in text area/input UIWebview

분류에서Dev

Move ResultSet cursor back in Firebird

분류에서Dev

How to simulate text input onchange when testing

분류에서Dev

After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

분류에서Dev

Column Selection in text area

분류에서Dev

Multi Color Text Area

분류에서Dev

Display text under cursor

분류에서Dev

Rails 6 ActionText rich_text_area works normally, does not appear when rendered in a layout

분류에서Dev

How to jump back to the last position of the cursor in emacs?

분류에서Dev

Keep table from expanding when text is input with PHP

분류에서Dev

read input text value when button click in table

분류에서Dev

How to preserve spaces in text input field when echoing php variable

분류에서Dev

Return a javascript stack trace for input when text is changed by script code

분류에서Dev

Validate value in text_area

분류에서Dev

Grab text area content with jQuery

분류에서Dev

Trigger jquery form validation if field is autofilled by browser

분류에서Dev

Browser's back button hidden by unity launcher

분류에서Dev

data not updating while clicking on browser back button

분류에서Dev

ruby on rails, redirect if hitting back button in browser

분류에서Dev

Full text Search of browser history, through the browser

분류에서Dev

Get Cursor Position With Input type number

분류에서Dev

show cursor/caret at the beginning of input box

분류에서Dev

How can I bring back icons on the notification to the sytem tray area?

분류에서Dev

Vim Colorscheme Weirdness for Background (Not-Text Area)

분류에서Dev

rails form_for edit text area

Related 관련 기사

  1. 1

    Trigger an event on back button in browser/android with javascript

  2. 2

    ActionListener to trigger on text input from JTextField

  3. 3

    ios7 webapp with appcache, when trigger history.back

  4. 4

    Display input from text area as HTML and assign IDs to the HTML elements

  5. 5

    Disable long press menu in text area/input UIWebview

  6. 6

    Move ResultSet cursor back in Firebird

  7. 7

    How to simulate text input onchange when testing

  8. 8

    After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

  9. 9

    Column Selection in text area

  10. 10

    Multi Color Text Area

  11. 11

    Display text under cursor

  12. 12

    Rails 6 ActionText rich_text_area works normally, does not appear when rendered in a layout

  13. 13

    How to jump back to the last position of the cursor in emacs?

  14. 14

    Keep table from expanding when text is input with PHP

  15. 15

    read input text value when button click in table

  16. 16

    How to preserve spaces in text input field when echoing php variable

  17. 17

    Return a javascript stack trace for input when text is changed by script code

  18. 18

    Validate value in text_area

  19. 19

    Grab text area content with jQuery

  20. 20

    Trigger jquery form validation if field is autofilled by browser

  21. 21

    Browser's back button hidden by unity launcher

  22. 22

    data not updating while clicking on browser back button

  23. 23

    ruby on rails, redirect if hitting back button in browser

  24. 24

    Full text Search of browser history, through the browser

  25. 25

    Get Cursor Position With Input type number

  26. 26

    show cursor/caret at the beginning of input box

  27. 27

    How can I bring back icons on the notification to the sytem tray area?

  28. 28

    Vim Colorscheme Weirdness for Background (Not-Text Area)

  29. 29

    rails form_for edit text area

뜨겁다태그

보관