Getting a user Enter key

126 views
Skip to first unread message

Greg Dougherty

unread,
Dec 3, 2010, 3:21:36 PM12/3/10
to Google Web Toolkit
Ok, the tutorial says that to get a user pressing an enter key in a
TextBox you should write something like the following:

http://code.google.com/webtoolkit/doc/latest/tutorial/manageevents.html

public void onKeyPress (KeyPressEvent event)
{
char keyPress = event.getCharCode();
int keyCode = keyPress;
if (keyPress == KeyCodes.KEY_ENTER)
goToRecord ();
}

Unfortunately, when I do that, I get a keyCode of 0 for Enter, Tab,
and Left Arrow (the keys I tested), while I get the actual key when I
type a number key. What gives? Is the tutorial wrong? If so, what
should I be calling?

TIA,

Greg

Brian Reilly

unread,
Dec 3, 2010, 3:36:52 PM12/3/10
to google-we...@googlegroups.com
Greg,

As I mentioned in a thread earlier today (http://groups.google.com/group/google-web-toolkit/browse_thread/thread/82a98103dfa76528), try using a KeyDownEvent handler and inspect event.getNativeEvent().getKeyCode() instead of event.getCharCode().

From the other thread, it sounds like this behavior may have changed in 2.1, so it could be that the documentation is wrong, but only as of fairly recently.

-Brian


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


David Chandler

unread,
Dec 3, 2010, 3:54:28 PM12/3/10
to google-we...@googlegroups.com
Also see http://code.google.com/p/google-web-toolkit/issues/detail?id=5558 for a workaround.

/dmc
--
David Chandler
Developer Programs Engineer, Google Web Toolkit
http://googlewebtoolkit.blogspot.com/

Greg Dougherty

unread,
Dec 3, 2010, 4:35:19 PM12/3/10
to Google Web Toolkit
Thank you both, that worked like a charm.

Greg

On Dec 3, 2:54 pm, David Chandler <drfibona...@google.com> wrote:
> Also seehttp://code.google.com/p/google-web-toolkit/issues/detail?id=5558for
> a workaround.
>
> /dmc
>
> On Fri, Dec 3, 2010 at 3:36 PM, Brian Reilly <brian.irei...@gmail.com>wrote:
>
>
>
> > Greg,
>
> > As I mentioned in a thread earlier today (
> >http://groups.google.com/group/google-web-toolkit/browse_thread/threa...),
> > try using a KeyDownEvent handler and inspect event.getNativeEvent().getKeyCode()
> > instead of event.getCharCode().
>
> > From the other thread, it sounds like this behavior may have changed in
> > 2.1, so it could be that the documentation is wrong, but only as of fairly
> > recently.
>
> > -Brian
>
> > On Fri, Dec 3, 2010 at 3:21 PM, Greg Dougherty <dougherty.greg...@mayo.edu
> > > wrote:
>
> >> Ok, the tutorial says that to get a user pressing an enter key in a
> >> TextBox you should write something like the following:
>
> >>http://code.google.com/webtoolkit/doc/latest/tutorial/manageevents.html
>
> >>        public void onKeyPress (KeyPressEvent event)
> >>        {
> >>                char    keyPress = event.getCharCode();
> >>                int     keyCode = keyPress;
> >>                if (keyPress == KeyCodes.KEY_ENTER)
> >>                        goToRecord ();
> >>        }
>
> >> Unfortunately, when I do that, I get a keyCode of 0 for  Enter, Tab,
> >> and Left Arrow (the keys I tested), while I get the actual key when I
> >> type a number key.  What gives?  Is the tutorial wrong?  If so, what
> >> should I be calling?
>
> >> TIA,
>
> >> Greg
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Google Web Toolkit" group.
> >> To post to this group, send email to google-we...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/google-web-toolkit?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .

Michael Phoenix

unread,
Jan 27, 2011, 11:16:56 PM1/27/11
to Google Web Toolkit
Hate to tell you, but your workaround is a broken link.

On Dec 3 2010, 12:54 pm, David Chandler <drfibona...@google.com>
wrote:
> Also seehttp://code.google.com/p/google-web-toolkit/issues/detail?id=5558for
> a workaround.
>
> /dmc
>
> On Fri, Dec 3, 2010 at 3:36 PM, Brian Reilly <brian.irei...@gmail.com>wrote:
>
>
>
> > Greg,
>
> > As I mentioned in a thread earlier today (
> >http://groups.google.com/group/google-web-toolkit/browse_thread/threa...),
> > try using a KeyDownEvent handler and inspect event.getNativeEvent().getKeyCode()
> > instead of event.getCharCode().
>
> > From the other thread, it sounds like this behavior may have changed in
> > 2.1, so it could be that the documentation is wrong, but only as of fairly
> > recently.
>
> > -Brian
>
> > On Fri, Dec 3, 2010 at 3:21 PM, Greg Dougherty <dougherty.greg...@mayo.edu
> > > wrote:
>
> >> Ok, the tutorial says that to get a user pressing an enter key in a
> >> TextBox you should write something like the following:
>
> >>http://code.google.com/webtoolkit/doc/latest/tutorial/manageevents.html
>
> >>        public void onKeyPress (KeyPressEvent event)
> >>        {
> >>                char    keyPress = event.getCharCode();
> >>                int     keyCode = keyPress;
> >>                if (keyPress == KeyCodes.KEY_ENTER)
> >>                        goToRecord ();
> >>        }
>
> >> Unfortunately, when I do that, I get a keyCode of 0 for  Enter, Tab,
> >> and Left Arrow (the keys I tested), while I get the actual key when I
> >> type a number key.  What gives?  Is the tutorial wrong?  If so, what
> >> should I be calling?
>
> >> TIA,
>
> >> Greg
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Google Web Toolkit" group.
> >> To post to this group, send email to google-we...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/google-web-toolkit?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .

Michael Phoenix

unread,
Jan 27, 2011, 11:27:11 PM1/27/11
to Google Web Toolkit
Well, I'm ran into this problem while doing the tutorial. I applied
your solution, but it still doesn't work properly. When I press enter
in the text box, the error alert comes up, but only for a fraction of
second, after which it disappears. It works fine for the button click.
Here's my pertinent code. Any idea what I might be missing? Google
should really update their tutorial so it works. This type of thing
doesn't encourage people who are new to the product to continue to use
it.

// Listen for keyboard events in the input box.
newSymbolTextBox.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
{
addStock();
}
}
});

}

/**
* Add stock to FlexTable. Executed when the user clicks the
addStockButton or
* presses enter in the newSymbolTextBox.
*/
private void addStock() {
final String symbol =
newSymbolTextBox.getText().toUpperCase().trim();
newSymbolTextBox.setFocus(true);

// Stock code must be between 1 and 10 chars that are numbers,
letters, or dots.
if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
Window.alert("'" + symbol + "' is not a valid symbol.");
newSymbolTextBox.selectAll();
return;
}

newSymbolTextBox.setText("");

// TODO Don't add the stock if it's already in the table.

// TODO Add the stock to the table.

// TODO Add a button to remove this stock from the table.

// TODO Get the stock price.


}

On Dec 3 2010, 12:36 pm, Brian Reilly <brian.irei...@gmail.com> wrote:
> Greg,
>
> As I mentioned in a thread earlier today (http://groups.google.com/group/google-web-toolkit/browse_thread/threa...),
> try using a KeyDownEvent handler and inspect
> event.getNativeEvent().getKeyCode()
> instead of event.getCharCode().
>
> From the other thread, it sounds like this behavior may have changed in 2.1,
> so it could be that the documentation is wrong, but only as of fairly
> recently.
>
> -Brian
>
> On Fri, Dec 3, 2010 at 3:21 PM, Greg Dougherty
> <dougherty.greg...@mayo.edu>wrote:
>
> > Ok, the tutorial says that to get a user pressing an enter key in a
> > TextBox you should write something like the following:
>
> >http://code.google.com/webtoolkit/doc/latest/tutorial/manageevents.html
>
> >        public void onKeyPress (KeyPressEvent event)
> >        {
> >                char    keyPress = event.getCharCode();
> >                int     keyCode = keyPress;
> >                if (keyPress == KeyCodes.KEY_ENTER)
> >                        goToRecord ();
> >        }
>
> > Unfortunately, when I do that, I get a keyCode of 0 for  Enter, Tab,
> > and Left Arrow (the keys I tested), while I get the actual key when I
> > type a number key.  What gives?  Is the tutorial wrong?  If so, what
> > should I be calling?
>
> > TIA,
>
> > Greg
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .

Brian Reilly

unread,
Jan 28, 2011, 2:03:01 PM1/28/11
to google-we...@googlegroups.com
Interesting... maybe the alert box is getting the key press event,
which isn't being fired until after the alert has popped up because
that's happening on key down. Maybe using
event.getNativeEvent().getKeyCode() in a KeyPressHandler will work.
I'm not sure because I haven't run into this case myself.

The workaround link that David Chandler posted is to issue #5558 in
the GWT issue tracker:

http://code.google.com/p/google-web-toolkit/issues/detail?id=5558

In particular, Thomas Broyer posted what looks like some nice code for
reliably detecting the enter key:

http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6

-Brian

> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Michael Phoenix

unread,
Jan 30, 2011, 3:38:40 AM1/30/11
to Google Web Toolkit
This is really weird. I just tried it again, without changing any code
and now it works fine!

On Jan 28, 11:03 am, Brian Reilly <brian.irei...@gmail.com> wrote:
> Interesting... maybe the alert box is getting the key press event,
> which isn't being fired until after the alert has popped up because
> that's happening on key down. Maybe using
> event.getNativeEvent().getKeyCode() in a KeyPressHandler will work.
> I'm not sure because I haven't run into this case myself.
>
> The workaround link that David Chandler posted is to issue #5558 in
> the GWT issue tracker:
>
>    http://code.google.com/p/google-web-toolkit/issues/detail?id=5558
>
> In particular, Thomas Broyer posted what looks like some nice code for
> reliably detecting theenterkey:
>
>    http://code.google.com/p/google-web-toolkit/issues/detail?id=5558#c6
>
> -Brian
>
> On Thu, Jan 27, 2011 at 11:27 PM, Michael Phoenix
>
> <michaelandrewphoe...@gmail.com> wrote:
> > Well, I'm ran into this problem while doing the tutorial. I applied
> > your solution, but it still doesn't work properly. When I pressenter
> > in the text box, the error alert comes up, but only for a fraction of
> > second, after which it disappears. It works fine for the button click.
> > Here's my pertinent code. Any idea what I might be missing? Google
> > should really update their tutorial so it works. This type of thing
> > doesn't encourage people who are new to the product to continue to use
> > it.
>
> >    // Listen for keyboard events in the input box.
> >    newSymbolTextBox.addKeyDownHandler(new KeyDownHandler() {
> >      public void onKeyDown(KeyDownEvent event) {
> >        if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
> > {
> >          addStock();
> >        }
> >      }
> >    });
>
> >  }
>
> >  /**
> >   * Add stock to FlexTable. Executed when the user clicks the
> > addStockButton or
> >   * pressesenterin the newSymbolTextBox.
> >> > Ok, the tutorial says that to get a user pressing anenterkey in a
Reply all
Reply to author
Forward
0 new messages