3

gwt 1.6.4 ie 8 ff 3.6.13

My users want to be able to hit enter to submit a form in a gwt TextBox. So I wrote the code, got it working then found that it double submitted (in firefox) So I took it out and noticed that hitting enter in firefox causes a page submit, but in IE it doesn't.

So either I have it half working (one of two popular browsers) or it works in ie and double submits in firefox.

Suggestions? I've seen lots of comments about this but nothing specific to gwt.

stu
  • 8,461
  • 18
  • 74
  • 112

2 Answers2

6
input.addKeyPressHandler(new KeyPressHandler()
            {
                @Override
                public void onKeyPress(KeyPressEvent event_)
                {
                    boolean enterPressed = KeyCodes.KEY_ENTER == event_
                            .getNativeEvent().getKeyCode();
                    if (enterPressed)
                    {
                        //submit logic here
                    }
                }
            });
Tom G
  • 2,025
  • 3
  • 21
  • 32
  • 2
    The `KeyPressHandler` should be used only get check 'ctrl', 'alt' or 'shift' buttons. This implementation will not work for Firefox as a result. You should use the `KeyDownHandler`. See [this answer](http://stackoverflow.com/a/4131910/2791703). – dARKpRINCE Jul 23 '15 at 12:54
1

Here is a handler that I developed to do a submit on enter that also tries to eliminate submission when the user uses enter to select an option in a field such as auto complete. It's not perfect, but it works. If the item I add to a form is an instance of FocusWidget I add the following handler.

protected final KeyPressHandler submitOnEnterHandler = new KeyPressHandler()
    {
        @Override
        public void onKeyPress(KeyPressEvent event)
        {
            char charCode = event.getCharCode();
            if (submitOnEnter && (charCode == '\n' || charCode == '\r'))
            {
                final Object source = event.getSource();
                final String beforeText;
                if (source instanceof TextBoxBase)
                    beforeText = ((TextBoxBase) source).getText();
                else
                    beforeText = null;
                Scheduler.get().scheduleDeferred(new ScheduledCommand()
                {
                    @Override
                    public void execute()
                    {
                        String afterText;
                        if (source instanceof TextBoxBase)
                            afterText = ((TextBoxBase) source).getText();
                        else
                            afterText = null;
                        if (beforeText.equals(afterText))
                            submit();
                    }
                });
            }
        }
    };
LINEMAN78
  • 2,562
  • 16
  • 19
  • while I'm inclined to agree with you, that hasn't been my actual experience. That may work with html input boxes, it doesn't seem to work in gwt. There's lots of messages on the web about how all the browsers act differently in this regard. – stu Mar 17 '11 at 11:15
  • Unless you are using a UI library GWT input boxes compile to HTML input boxes. I will check my personal libraries on monday to ensure that I don't have key handlers in my form, but if I remember correctly I went down that road and realized I was replicating functionality and also running into minor problems where pressing enter to select an autocomplete value was submitting the form unexpectedly. Also, there is a minor problem where some of the key handlers don't fire the key value in IE. – LINEMAN78 Mar 18 '11 at 07:26
  • Then I must be doing something else wrong. I certainly remember in the good old days where hitting enter submitted the form, but it ain't happening now. It might be because I don't have an honest to god submit button. I use a button type=button with an onclick to submit my form. – stu Mar 21 '11 at 13:40
  • Updated my answer with the source from my personal lib. – LINEMAN78 Mar 28 '11 at 22:36